[llvm] Fixed AddressSpaceCast in CodeExtractor (PR #96230)

Eric Wright via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 20 12:26:36 PDT 2024


https://github.com/efwright created https://github.com/llvm/llvm-project/pull/96230

In the CodeExtractor if aggregate arguments are used it allocates a struct to hold variables needed to be passed into an outlined function. Then, if this alloca was not in the default address space an AddressSpaceCast was emitted. However, this new instruction was unused, and the code instead just continued to reference the original alloca instruction.

This PR changes it so that the AddressSpaceCast is actually used instead of the original alloca.

>From 1b8480def08a5951e21548f4d428225a643e4cb8 Mon Sep 17 00:00:00 2001
From: Eric Francis Wright <wright117 at rzvernal33.llnl.gov>
Date: Thu, 20 Jun 2024 12:17:23 -0700
Subject: [PATCH] Fixed AddressSpaceCast in CodeExtractor

---
 llvm/lib/Transforms/Utils/CodeExtractor.cpp | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
index b2775eb6c6c7a..2d4d8e62bf3c5 100644
--- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp
+++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
@@ -1195,7 +1195,7 @@ CallInst *CodeExtractor::emitCallAndSwitchStatement(Function *newFunction,
   }
 
   StructType *StructArgTy = nullptr;
-  AllocaInst *Struct = nullptr;
+  Instruction *Struct = nullptr;
   unsigned NumAggregatedInputs = 0;
   if (AggregateArgs && !StructValues.empty()) {
     std::vector<Type *> ArgTypes;
@@ -1204,19 +1204,20 @@ CallInst *CodeExtractor::emitCallAndSwitchStatement(Function *newFunction,
 
     // Allocate a struct at the beginning of this function
     StructArgTy = StructType::get(newFunction->getContext(), ArgTypes);
-    Struct = new AllocaInst(
+    auto *StructAlloca = new AllocaInst(
         StructArgTy, DL.getAllocaAddrSpace(), nullptr, "structArg",
         AllocationBlock ? AllocationBlock->getFirstInsertionPt()
                         : codeReplacer->getParent()->front().begin());
 
     if (ArgsInZeroAddressSpace && DL.getAllocaAddrSpace() != 0) {
       auto *StructSpaceCast = new AddrSpaceCastInst(
-          Struct, PointerType ::get(Context, 0), "structArg.ascast");
-      StructSpaceCast->insertAfter(Struct);
-      params.push_back(StructSpaceCast);
+          StructAlloca, PointerType ::get(Context, 0), "structArg.ascast");
+      StructSpaceCast->insertAfter(StructAlloca);
+      Struct = StructSpaceCast;
     } else {
-      params.push_back(Struct);
+      Struct = StructAlloca;
     }
+    params.push_back(Struct);
     // Store aggregated inputs in the struct.
     for (unsigned i = 0, e = StructValues.size(); i != e; ++i) {
       if (inputs.contains(StructValues[i])) {



More information about the llvm-commits mailing list