[llvm] [Linker] Propagate `nobuiltin` attributes when linking known libcalls (PR #89431)

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 19 12:52:35 PDT 2024


================
@@ -1605,14 +1611,27 @@ Error IRLinker::run() {
 
   DstM.setTargetTriple(SrcTriple.merge(DstTriple));
 
+  // Update the target triple's libcall information if it was changed.
+  Libcalls.updateLibcalls(Triple(DstM.getTargetTriple()));
+
   // Loop over all of the linked values to compute type mappings.
   computeTypeMapping();
 
+  bool AddsLibcalls;
   std::reverse(Worklist.begin(), Worklist.end());
   while (!Worklist.empty()) {
     GlobalValue *GV = Worklist.back();
     Worklist.pop_back();
 
+    // If the module already contains libcall functions we need every function
+    // linked in to have `nobuiltin` attributes. Otherwise check if this is a
+    // libcall definition.
+    if (Function *F = dyn_cast<Function>(GV); F && Libcalls.hasLibcalls())
+      F->setAttributes(F->getAttributes().addFnAttribute(F->getContext(),
+                                                         Attribute::NoBuiltin));
----------------
jhuber6 wrote:

No, there are two cases.

1. Linking in a module that contains libcalls.

When this happens we need to modify *all* definitions in the destination / merged module. However, once we do this once we know that the merged module is already processed.

2. Linking a module into a module that already contains libcalls

Here we only need to add the attributes to the definitions as they come in because we know the destination module has already been handled (If the boolean is set true). I did it this way to avoid looping over every single function that we've already touched. I could make it do that if you think the code clarity is better.

https://github.com/llvm/llvm-project/pull/89431


More information about the llvm-commits mailing list