HsiangKai created this revision. HsiangKai added reviewers: craig.topper, frasercrmck, rogfer01. Herald added subscribers: StephenFan, luismarques, apazos, sameer.abuasal, s.egerton, Jim, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, edward-jones, zzheng, jrtc27, niosHD, sabuasal, simoncook, johnrusso, rbar, asb. HsiangKai requested review of this revision. Herald added subscribers: cfe-commits, MaskRay. Herald added a project: clang.
If the memory object is scalable type, we do not know the exact size of it at compile time. Set the size of lifetime marker to unknown if the object is scalable one. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D102822 Files: clang/lib/CodeGen/CGCall.cpp clang/lib/CodeGen/CGDecl.cpp clang/lib/CodeGen/CGExprAgg.cpp clang/lib/CodeGen/CodeGenFunction.h clang/test/CodeGen/RISCV/riscv-v-lifetime.c
Index: clang/test/CodeGen/RISCV/riscv-v-lifetime.c =================================================================== --- /dev/null +++ clang/test/CodeGen/RISCV/riscv-v-lifetime.c @@ -0,0 +1,35 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py +// REQUIRES: riscv-registered-target +// RUN: %clang_cc1 -triple riscv64 -target-feature +experimental-v -emit-llvm \ +// RUN: -O1 -o - %s | FileCheck %s + +#include <riscv_vector.h> + +extern void use(vint32m1_t *a); + +// CHECK-LABEL: @inlined( +// CHECK-NEXT: entry: +// CHECK-NEXT: [[A:%.*]] = alloca <vscale x 2 x i32>, align 4 +// CHECK-NEXT: [[TMP0:%.*]] = bitcast <vscale x 2 x i32>* [[A]] to i8* +// CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull [[TMP0]]) #[[ATTR4:[0-9]+]] +// CHECK-NEXT: call void @use(<vscale x 2 x i32>* nonnull [[A]]) #[[ATTR4]] +// CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull [[TMP0]]) #[[ATTR4]] +// CHECK-NEXT: ret void +// +__attribute__((always_inline)) void inlined() { + vint32m1_t a; + use(&a); +} + +// CHECK-LABEL: @lifetime_test( +// CHECK-NEXT: entry: +// CHECK-NEXT: [[A_I:%.*]] = alloca <vscale x 2 x i32>, align 4 +// CHECK-NEXT: [[TMP0:%.*]] = bitcast <vscale x 2 x i32>* [[A_I]] to i8* +// CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* nonnull [[TMP0]]) #[[ATTR4]] +// CHECK-NEXT: call void @use(<vscale x 2 x i32>* nonnull [[A_I]]) #[[ATTR4]] +// CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 -1, i8* nonnull [[TMP0]]) #[[ATTR4]] +// CHECK-NEXT: ret void +// +void lifetime_test() { + inlined(); +} Index: clang/lib/CodeGen/CodeGenFunction.h =================================================================== --- clang/lib/CodeGen/CodeGenFunction.h +++ clang/lib/CodeGen/CodeGenFunction.h @@ -2872,7 +2872,7 @@ void EmitSehTryScopeBegin(); void EmitSehTryScopeEnd(); - llvm::Value *EmitLifetimeStart(uint64_t Size, llvm::Value *Addr); + llvm::Value *EmitLifetimeStart(llvm::TypeSize Size, llvm::Value *Addr); void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr); llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E); Index: clang/lib/CodeGen/CGExprAgg.cpp =================================================================== --- clang/lib/CodeGen/CGExprAgg.cpp +++ clang/lib/CodeGen/CGExprAgg.cpp @@ -276,7 +276,7 @@ RetAddr = Dest.getAddress(); } else { RetAddr = CGF.CreateMemTemp(RetTy, "tmp", &RetAllocaAddr); - uint64_t Size = + llvm::TypeSize Size = CGF.CGM.getDataLayout().getTypeAllocSize(CGF.ConvertTypeForMem(RetTy)); LifetimeSizePtr = CGF.EmitLifetimeStart(Size, RetAllocaAddr.getPointer()); if (LifetimeSizePtr) { Index: clang/lib/CodeGen/CGDecl.cpp =================================================================== --- clang/lib/CodeGen/CGDecl.cpp +++ clang/lib/CodeGen/CGDecl.cpp @@ -1317,11 +1317,15 @@ /// Emit a lifetime.begin marker if some criteria are satisfied. /// \return a pointer to the temporary size Value if a marker was emitted, null /// otherwise -llvm::Value *CodeGenFunction::EmitLifetimeStart(uint64_t Size, +llvm::Value *CodeGenFunction::EmitLifetimeStart(llvm::TypeSize Size, llvm::Value *Addr) { if (!ShouldEmitLifetimeMarkers) return nullptr; + // Use -1 as the unknown size. + if (Size.isScalable()) + Size = llvm::TypeSize::Fixed(-1); + assert(Addr->getType()->getPointerAddressSpace() == CGM.getDataLayout().getAllocaAddrSpace() && "Pointer should be in alloca address space"); @@ -1551,9 +1555,7 @@ llvm::TypeSize size = CGM.getDataLayout().getTypeAllocSize(allocaTy); emission.SizeForLifetimeMarkers = - size.isScalable() ? EmitLifetimeStart(-1, AllocaAddr.getPointer()) - : EmitLifetimeStart(size.getFixedSize(), - AllocaAddr.getPointer()); + EmitLifetimeStart(size, AllocaAddr.getPointer()); } } else { assert(!emission.useLifetimeMarkers()); Index: clang/lib/CodeGen/CGCall.cpp =================================================================== --- clang/lib/CodeGen/CGCall.cpp +++ clang/lib/CodeGen/CGCall.cpp @@ -4679,7 +4679,7 @@ } else { SRetPtr = CreateMemTemp(RetTy, "tmp", &SRetAlloca); if (HaveInsertPoint() && ReturnValue.isUnused()) { - uint64_t size = + llvm::TypeSize size = CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy)); UnusedReturnSizePtr = EmitLifetimeStart(size, SRetAlloca.getPointer()); } @@ -4840,7 +4840,7 @@ IRCallArgs[FirstIRArg] = AI.getPointer(); // Emit lifetime markers for the temporary alloca. - uint64_t ByvalTempElementSize = + llvm::TypeSize ByvalTempElementSize = CGM.getDataLayout().getTypeAllocSize(AI.getElementType()); llvm::Value *LifetimeSize = EmitLifetimeStart(ByvalTempElementSize, AI.getPointer());
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits