LTO: always parse modules in opaque pointer mode.
authorTim Northover <tnorthover@apple.com>
Tue, 13 Dec 2022 09:10:56 +0000 (09:10 +0000)
committerTim Northover <tnorthover@apple.com>
Wed, 14 Dec 2022 14:13:08 +0000 (14:13 +0000)
Once an LLVMContext has been told it needs to track pointer types, it can no
longer be used to parse opaque modules. However, we are likely  (at least for a
while) to have old LTO .o files in the SDK that need to interoperate with
just-generated ones, so deciding opaqueness based on the first module read
causes linker failures.

This makes the llvm-c LTO interface parse any object it sees in opaque mode,
even if type data is present, which guarantees compatibility.

llvm/tools/lto/lto.cpp

index f50b6ac..a2d0512 100644 (file)
@@ -106,6 +106,7 @@ static void lto_initialize() {
 
     static LLVMContext Context;
     LTOContext = &Context;
+    LTOContext->setOpaquePointers(true);
     LTOContext->setDiagnosticHandler(
         std::make_unique<LTOToolDiagnosticHandler>(), true);
     initialized = true;
@@ -133,7 +134,10 @@ struct LibLTOCodeGenerator : LTOCodeGenerator {
   // Module must be destructed before its context gets destructed.
   ~LibLTOCodeGenerator() { resetMergedModule(); }
 
-  void init() { setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
+  void init() {
+    OwnedContext->setOpaquePointers(true);
+    setDiagnosticHandler(handleLibLTODiagnostic, nullptr);
+  }
 
   std::unique_ptr<MemoryBuffer> NativeObjectFile;
   std::unique_ptr<LLVMContext> OwnedContext;
@@ -271,6 +275,7 @@ lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
 
   // Create a local context. Ownership will be transferred to LTOModule.
   std::unique_ptr<LLVMContext> Context = std::make_unique<LLVMContext>();
+  Context->setOpaquePointers(true);
   Context->setDiagnosticHandler(std::make_unique<LTOToolDiagnosticHandler>(),
                                 true);