[clang] a067891 - [clang][codegen] Fix another lifetime emission on alloca on non-default address space.

Michael Liao via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 9 21:27:18 PST 2020


Author: Michael Liao
Date: 2020-02-10T00:15:56-05:00
New Revision: a06789138987d1f64bb2f97d3a5c0f39eaf94715

URL: https://github.com/llvm/llvm-project/commit/a06789138987d1f64bb2f97d3a5c0f39eaf94715
DIFF: https://github.com/llvm/llvm-project/commit/a06789138987d1f64bb2f97d3a5c0f39eaf94715.diff

LOG: [clang][codegen] Fix another lifetime emission on alloca on non-default address space.

- Lifetime intrinsics expect the pointer directly from alloca. Need
  extra handling for targets with alloca on non-default (or non-zero)
  address space.

Added: 
    clang/test/CodeGenCXX/amdgcn-call-with-aggarg.cpp

Modified: 
    clang/lib/CodeGen/CGCall.cpp
    clang/lib/CodeGen/CodeGenFunction.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3edcfb21ef34..9ef2a3b3d099 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -3690,8 +3690,9 @@ void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
   }
 
   AggValueSlot ArgSlot = AggValueSlot::ignored();
+  Address ArgSlotAlloca = Address::invalid();
   if (hasAggregateEvaluationKind(E->getType())) {
-    ArgSlot = CreateAggTemp(E->getType(), "agg.tmp");
+    ArgSlot = CreateAggTemp(E->getType(), "agg.tmp", &ArgSlotAlloca);
 
     // Emit a lifetime start/end for this temporary. If the type has a
     // destructor, then we need to keep it alive. FIXME: We should still be able
@@ -3699,8 +3700,9 @@ void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
     if (!E->getType().isDestructedType()) {
       uint64_t size =
           CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(E->getType()));
-      if (auto *lifetimeSize = EmitLifetimeStart(size, ArgSlot.getPointer()))
-        args.addLifetimeCleanup({ArgSlot.getPointer(), lifetimeSize});
+      if (auto *lifetimeSize =
+              EmitLifetimeStart(size, ArgSlotAlloca.getPointer()))
+        args.addLifetimeCleanup({ArgSlotAlloca.getPointer(), lifetimeSize});
     }
   }
 

diff  --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index f48d8a4cc366..7ddd38c7b262 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -2264,8 +2264,9 @@ class CodeGenFunction : public CodeGenTypeCache {
 
   /// CreateAggTemp - Create a temporary memory object for the given
   /// aggregate type.
-  AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
-    return AggValueSlot::forAddr(CreateMemTemp(T, Name),
+  AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp",
+                             Address *Alloca = nullptr) {
+    return AggValueSlot::forAddr(CreateMemTemp(T, Name, Alloca),
                                  T.getQualifiers(),
                                  AggValueSlot::IsNotDestructed,
                                  AggValueSlot::DoesNotNeedGCBarriers,

diff  --git a/clang/test/CodeGenCXX/amdgcn-call-with-aggarg.cpp b/clang/test/CodeGenCXX/amdgcn-call-with-aggarg.cpp
new file mode 100644
index 000000000000..e9d3683cfaa2
--- /dev/null
+++ b/clang/test/CodeGenCXX/amdgcn-call-with-aggarg.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -emit-llvm -O3 -disable-llvm-passes -o - %s | FileCheck %s
+
+struct A {
+  float x, y, z, w;
+};
+
+void foo(A a);
+
+// CHECK-LABEL: @_Z4testv
+// CHECK: %[[lvar:.*]] = alloca %struct.A, align 4, addrspace(5)
+// CHECK: %[[atmp:.*]] = alloca %struct.A, align 4, addrspace(5)
+// CHECK: %[[lcst:.*]] = bitcast %struct.A addrspace(5)* %[[lvar]] to i8 addrspace(5)*
+// CHECK: call void @llvm.lifetime.start.p5i8(i64 16, i8 addrspace(5)* %[[lcst]]
+// CHECK: %[[acst:.*]] = bitcast %struct.A addrspace(5)* %[[atmp]] to i8 addrspace(5)*
+// CHECK: call void @llvm.lifetime.start.p5i8(i64 16, i8 addrspace(5)* %[[acst]]
+void test() {
+  A a;
+  foo(a);
+}


        


More information about the cfe-commits mailing list