Skip to content

Commit 6e6d3a0

Browse files
committed
add lifetime op
1 parent 424918e commit 6e6d3a0

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5721,6 +5721,18 @@ def LinkerOptionsOp : CIR_Op<"linker_options"> {
57215721
let hasVerifier = 1;
57225722
}
57235723

5724+
//===----------------------------------------------------------------------===//
5725+
// LifetimeStartOp & LifetimeEndOp
5726+
//===----------------------------------------------------------------------===//
5727+
5728+
class CIR_LifetimeBaseOp<string mnemonic> : CIR_Op<mnemonic, []> {
5729+
let arguments = (ins I64Attr:$size, CIR_PointerType:$ptr);
5730+
let assemblyFormat = "$size `,` $ptr attr-dict `:` qualified(type($ptr))";
5731+
}
5732+
5733+
def LifetimeStartOp : CIR_LifetimeBaseOp<"lifetime.start">;
5734+
def LifetimeEndOp : CIR_LifetimeBaseOp<"lifetime.end">;
5735+
57245736
//===----------------------------------------------------------------------===//
57255737
// Standard library function calls
57265738
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,27 @@ void CIRGenFunction::emitExprAsInit(const Expr *init, const ValueDecl *D,
763763
llvm_unreachable("bad evaluation kind");
764764
}
765765

766+
mlir::Attribute CIRGenFunction::emitLifetimeStart(int64_t size, mlir::Value addr) {
767+
assert(!cir::MissingFeatures::shouldEmitLifetimeMarkers());
768+
if (!shouldEmitLifetimeMarkers)
769+
return nullptr;
770+
assert(cast<cir::PointerType>(addr.getType()).getAddrSpace() ==
771+
CGM.getCIRAllocaAddressSpace() &&
772+
"Pointer should be in alloca address space");
773+
mlir::IntegerAttr sizeAttr = builder.getI64IntegerAttr(size);
774+
builder.create<cir::LifetimeStartOp>(*currSrcLoc, sizeAttr, addr);
775+
return sizeAttr;
776+
}
777+
778+
void CIRGenFunction::emitLifetimeEnd(int64_t size, mlir::Value addr) {
779+
assert(!cir::MissingFeatures::shouldEmitLifetimeMarkers());
780+
assert(cast<cir::PointerType>(addr.getType()).getAddrSpace() ==
781+
CGM.getCIRAllocaAddressSpace() &&
782+
"Pointer should be in alloca address space");
783+
mlir::IntegerAttr sizeAttr = builder.getI64IntegerAttr(size);
784+
builder.create<cir::LifetimeEndOp>(*currSrcLoc, sizeAttr, addr);
785+
}
786+
766787
void CIRGenFunction::emitDecl(const Decl &D) {
767788
switch (D.getKind()) {
768789
case Decl::ImplicitConceptSpecialization:

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2349,7 +2349,7 @@ LValue CIRGenFunction::emitMaterializeTemporaryExpr(
23492349
break;
23502350

23512351
case SD_FullExpression: {
2352-
if (!ShouldEmitLifetimeMarkers)
2352+
if (!shouldEmitLifetimeMarkers)
23532353
break;
23542354
assert(0 && "NYI");
23552355
break;

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ CIRGenFunction::CIRGenFunction(CIRGenModule &cgm, CIRGenBuilderTy &builder,
4040
bool suppressNewContext)
4141
: CIRGenTypeCache(cgm), CGM{cgm}, builder(builder),
4242
SanOpts(cgm.getLangOpts().Sanitize), CurFPFeatures(cgm.getLangOpts()),
43-
ShouldEmitLifetimeMarkers(false) {
43+
shouldEmitLifetimeMarkers(false) {
4444
if (!suppressNewContext)
4545
cgm.getCXXABI().getMangleContext().startNewFunction();
4646
EHStack.setCGF(this);
@@ -709,7 +709,7 @@ cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl gd, cir::FuncOp fn,
709709

710710
// Initialize helper which will detect jumps which can cause invalid
711711
// lifetime markers.
712-
if (ShouldEmitLifetimeMarkers)
712+
if (shouldEmitLifetimeMarkers)
713713
assert(!cir::MissingFeatures::shouldEmitLifetimeMarkers() && "NYI");
714714
}
715715

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ class CIRGenFunction : public CIRGenTypeCache {
510510
SymTableTy symbolTable;
511511
/// True if we need to emit the life-time markers. This is initially set in
512512
/// the constructor, but could be overwrriten to true if this is a coroutine.
513-
bool ShouldEmitLifetimeMarkers;
513+
bool shouldEmitLifetimeMarkers;
514514

515515
using DeclMapTy = llvm::DenseMap<const clang::Decl *, Address>;
516516
/// This keeps track of the CIR allocas or globals for local C
@@ -2490,6 +2490,9 @@ class CIRGenFunction : public CIRGenTypeCache {
24902490

24912491
mlir::Value emitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
24922492

2493+
mlir::Attribute emitLifetimeStart(int64_t size, mlir::Value addr);
2494+
void emitLifetimeEnd(int64_t size, mlir::Value addr);
2495+
24932496
/// CIR build helpers
24942497
/// -----------------
24952498
public:

0 commit comments

Comments
 (0)