[ORC] Drop Comdat when discarding IR symbol
authorJonas Hahnfeld <jonas.hahnfeld@cern.ch>
Tue, 24 Jan 2023 09:54:55 +0000 (10:54 +0100)
committerJonas Hahnfeld <jonas.hahnfeld@cern.ch>
Fri, 3 Feb 2023 07:41:58 +0000 (08:41 +0100)
According to the IR verifier, "Declaration[s] may not be in a Comdat!"

Differential Revision: https://reviews.llvm.org/D142443

llvm/lib/ExecutionEngine/Orc/Layer.cpp
llvm/test/ExecutionEngine/Orc/Inputs/weak-comdat-def.ll [new file with mode: 0644]
llvm/test/ExecutionEngine/Orc/weak-comdat.ll [new file with mode: 0644]

index 95380d9..3368d32 100644 (file)
@@ -125,6 +125,10 @@ void IRMaterializationUnit::discard(const JITDylib &JD,
   assert(!I->second->isDeclaration() &&
          "Discard should only apply to definitions");
   I->second->setLinkage(GlobalValue::AvailableExternallyLinkage);
+  // According to the IR verifier, "Declaration[s] may not be in a Comdat!"
+  // Remove it, if this is a GlobalObject.
+  if (auto *GO = dyn_cast<GlobalObject>(I->second))
+    GO->setComdat(nullptr);
   SymbolToDefinition.erase(I);
 }
 
diff --git a/llvm/test/ExecutionEngine/Orc/Inputs/weak-comdat-def.ll b/llvm/test/ExecutionEngine/Orc/Inputs/weak-comdat-def.ll
new file mode 100644 (file)
index 0000000..9c308a3
--- /dev/null
@@ -0,0 +1,6 @@
+$c = comdat any
+
+define i32 @f() comdat($c) {
+entry:
+  ret i32 0
+}
diff --git a/llvm/test/ExecutionEngine/Orc/weak-comdat.ll b/llvm/test/ExecutionEngine/Orc/weak-comdat.ll
new file mode 100644 (file)
index 0000000..d11e462
--- /dev/null
@@ -0,0 +1,14 @@
+; RUN: lli -extra-module %p/Inputs/weak-comdat-def.ll %s
+
+$c = comdat any
+
+define weak i32 @f() comdat($c) {
+entry:
+  ret i32 0
+}
+
+define i32 @main() {
+entry:
+  %0 = call i32 @f()
+  ret i32 %0
+}