Skip to content

Commit 3edeb5d

Browse files
authored
[CIR] Streamline unit attribute setters using pre-generated ones (#1851)
1 parent bec5b92 commit 3edeb5d

File tree

8 files changed

+37
-36
lines changed

8 files changed

+37
-36
lines changed

clang/lib/CIR/CodeGen/CIRGenAtomic.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,8 @@ static void emitAtomicOp(CIRGenFunction &CGF, AtomicExpr *E, Address Dest,
594594
auto load = builder.createLoad(loc, Ptr);
595595
// FIXME(cir): add scope information.
596596
assert(!cir::MissingFeatures::syncScopeID());
597-
load->setAttr("mem_order", orderAttr);
598-
if (E->isVolatile())
599-
load->setAttr("is_volatile", mlir::UnitAttr::get(builder.getContext()));
597+
load.setMemOrder(Order);
598+
load.setIsVolatile(E->isVolatile());
600599

601600
// TODO(cir): this logic should be part of createStore, but doing so
602601
// currently breaks CodeGen/union.cpp and CodeGen/union.cpp.

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,8 @@ RValue CIRGenFunction::emitRotate(const CallExpr *E, bool IsRotateRight) {
443443
// result, but the CIR ops uses the same type for all values.
444444
auto ty = src.getType();
445445
shiftAmt = builder.createIntCast(shiftAmt, ty);
446-
auto r =
447-
builder.create<cir::RotateOp>(getLoc(E->getSourceRange()), src, shiftAmt);
448-
if (!IsRotateRight)
449-
r->setAttr("left", mlir::UnitAttr::get(src.getContext()));
446+
auto r = cir::RotateOp::create(builder, getLoc(E->getSourceRange()), src,
447+
shiftAmt, !IsRotateRight);
450448
return RValue::get(r);
451449
}
452450

@@ -2382,7 +2380,7 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
23822380
auto fnOp =
23832381
CGM.GetOrCreateCIRFunction(ND->getName(), ty, gd, /*ForVTable=*/false,
23842382
/*DontDefer=*/false);
2385-
fnOp.setBuiltinAttr(mlir::UnitAttr::get(&getMLIRContext()));
2383+
fnOp.setBuiltin(true);
23862384
return emitCall(E->getCallee()->getType(), CIRGenCallee::forDirect(fnOp), E,
23872385
ReturnValue);
23882386
}

clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,11 @@ cir::CallOp CIRGenFunction::emitCoroIDBuiltinCall(mlir::Location loc,
169169

170170
cir::FuncOp fnOp;
171171
if (!builtin) {
172-
fnOp = CGM.createCIRFunction(
172+
fnOp = CGM.createCIRBuiltinFunction(
173173
loc, CGM.builtinCoroId,
174174
cir::FuncType::get({int32Ty, VoidPtrTy, VoidPtrTy, VoidPtrTy}, int32Ty),
175175
/*FD=*/nullptr);
176176
assert(fnOp && "should always succeed");
177-
fnOp.setBuiltinAttr(mlir::UnitAttr::get(&getMLIRContext()));
178177
} else
179178
fnOp = cast<cir::FuncOp>(builtin);
180179

@@ -191,11 +190,10 @@ cir::CallOp CIRGenFunction::emitCoroAllocBuiltinCall(mlir::Location loc) {
191190

192191
cir::FuncOp fnOp;
193192
if (!builtin) {
194-
fnOp = CGM.createCIRFunction(loc, CGM.builtinCoroAlloc,
195-
cir::FuncType::get({int32Ty}, boolTy),
196-
/*FD=*/nullptr);
193+
fnOp = CGM.createCIRBuiltinFunction(loc, CGM.builtinCoroAlloc,
194+
cir::FuncType::get({int32Ty}, boolTy),
195+
/*FD=*/nullptr);
197196
assert(fnOp && "should always succeed");
198-
fnOp.setBuiltinAttr(mlir::UnitAttr::get(&getMLIRContext()));
199197
} else
200198
fnOp = cast<cir::FuncOp>(builtin);
201199

@@ -211,12 +209,11 @@ CIRGenFunction::emitCoroBeginBuiltinCall(mlir::Location loc,
211209

212210
cir::FuncOp fnOp;
213211
if (!builtin) {
214-
fnOp = CGM.createCIRFunction(
212+
fnOp = CGM.createCIRBuiltinFunction(
215213
loc, CGM.builtinCoroBegin,
216214
cir::FuncType::get({int32Ty, VoidPtrTy}, VoidPtrTy),
217215
/*FD=*/nullptr);
218216
assert(fnOp && "should always succeed");
219-
fnOp.setBuiltinAttr(mlir::UnitAttr::get(&getMLIRContext()));
220217
} else
221218
fnOp = cast<cir::FuncOp>(builtin);
222219

@@ -232,12 +229,11 @@ cir::CallOp CIRGenFunction::emitCoroEndBuiltinCall(mlir::Location loc,
232229

233230
cir::FuncOp fnOp;
234231
if (!builtin) {
235-
fnOp =
236-
CGM.createCIRFunction(loc, CGM.builtinCoroEnd,
237-
cir::FuncType::get({VoidPtrTy, boolTy}, boolTy),
238-
/*FD=*/nullptr);
232+
fnOp = CGM.createCIRBuiltinFunction(
233+
loc, CGM.builtinCoroEnd,
234+
cir::FuncType::get({VoidPtrTy, boolTy}, boolTy),
235+
/*FD=*/nullptr);
239236
assert(fnOp && "should always succeed");
240-
fnOp.setBuiltinAttr(mlir::UnitAttr::get(&getMLIRContext()));
241237
} else
242238
fnOp = cast<cir::FuncOp>(builtin);
243239

@@ -252,7 +248,7 @@ CIRGenFunction::emitCoroutineBody(const CoroutineBodyStmt &S) {
252248

253249
auto Fn = dyn_cast<cir::FuncOp>(CurFn);
254250
assert(Fn && "other callables NYI");
255-
Fn.setCoroutineAttr(mlir::UnitAttr::get(&getMLIRContext()));
251+
Fn.setCoroutine(true);
256252
auto coroId = emitCoroIDBuiltinCall(openCurlyLoc, nullPtrCst);
257253
createCoroData(*this, CurCoro, coroId);
258254

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,7 @@ void CIRGenFunction::emitAutoVarInit(const AutoVarEmission &emission) {
334334
auto allocaOp = addr.getDefiningOp<cir::AllocaOp>();
335335
assert(allocaOp && "Address should come straight out of the alloca");
336336

337-
if (!allocaOp.use_empty())
338-
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
337+
allocaOp.setInit(!allocaOp.use_empty());
339338
return;
340339
}
341340

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,7 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
655655
if (currVarDecl && SrcAlloca) {
656656
const VarDecl *VD = currVarDecl;
657657
assert(VD && "VarDecl expected");
658-
if (VD->hasInit())
659-
SrcAlloca.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
658+
SrcAlloca.setInit(VD->hasInit());
660659
}
661660

662661
assert(currSrcLoc && "must pass in source location");
@@ -1329,7 +1328,7 @@ LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *E) {
13291328

13301329
// Tag 'load' with deref attribute.
13311330
if (auto loadOp = Addr.getDefiningOp<cir::LoadOp>()) {
1332-
loadOp.setIsDerefAttr(mlir::UnitAttr::get(&getMLIRContext()));
1331+
loadOp.setIsDeref(true);
13331332
}
13341333

13351334
LValue LV = LValue::makeAddr(Addr, T, BaseInfo, TBAAInfo);

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,8 @@ mlir::LogicalResult CIRGenFunction::declare(const Decl *var, QualType ty,
296296

297297
addr = emitAlloca(namedVar->getName(), ty, loc, alignment);
298298
auto allocaOp = addr.getDefiningOp<cir::AllocaOp>();
299-
if (isParam)
300-
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
301-
if (ty->isReferenceType() || ty.isConstQualified())
302-
allocaOp.setConstantAttr(mlir::UnitAttr::get(&getMLIRContext()));
299+
allocaOp.setInit(isParam);
300+
allocaOp.setConstant(ty->isReferenceType() || ty.isConstQualified());
303301

304302
symbolTable.insert(var, addr);
305303
return mlir::success();
@@ -316,10 +314,8 @@ mlir::LogicalResult CIRGenFunction::declare(Address addr, const Decl *var,
316314

317315
addrVal = addr.getPointer();
318316
auto allocaOp = addrVal.getDefiningOp<cir::AllocaOp>();
319-
if (isParam)
320-
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
321-
if (ty->isReferenceType() || ty.isConstQualified())
322-
allocaOp.setConstantAttr(mlir::UnitAttr::get(&getMLIRContext()));
317+
allocaOp.setInit(isParam);
318+
allocaOp.setConstant(ty->isReferenceType() || ty.isConstQualified());
323319

324320
symbolTable.insert(var, addrVal);
325321
return mlir::success();
@@ -1350,7 +1346,7 @@ void CIRGenFunction::StartFunction(GlobalDecl gd, QualType retTy,
13501346
// We're in a lambda.
13511347
auto fn = dyn_cast<cir::FuncOp>(CurFn);
13521348
assert(fn && "other callables NYI");
1353-
fn.setLambdaAttr(mlir::UnitAttr::get(&getMLIRContext()));
1349+
fn.setLambda(true);
13541350

13551351
// Figure out the captures.
13561352
md->getParent()->getCaptureFields(LambdaCaptureFields,

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,15 @@ cir::FuncOp CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
27742774
return f;
27752775
}
27762776

2777+
cir::FuncOp
2778+
CIRGenModule::createCIRBuiltinFunction(mlir::Location loc, StringRef name,
2779+
cir::FuncType ty,
2780+
const clang::FunctionDecl *fd) {
2781+
cir::FuncOp fnOp = createCIRFunction(loc, name, ty, fd);
2782+
fnOp.setBuiltin(true);
2783+
return fnOp;
2784+
}
2785+
27772786
cir::FuncOp CIRGenModule::createRuntimeFunction(cir::FuncType ty,
27782787
StringRef name, mlir::ArrayAttr,
27792788
[[maybe_unused]] bool local,

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,11 @@ class CIRGenModule : public CIRGenTypeCache {
870870
cir::FuncType Ty,
871871
const clang::FunctionDecl *FD);
872872

873+
/// Create a CIR function with builtin attribute set.
874+
cir::FuncOp createCIRBuiltinFunction(mlir::Location loc, llvm::StringRef name,
875+
cir::FuncType Ty,
876+
const clang::FunctionDecl *FD);
877+
873878
cir::FuncOp createRuntimeFunction(cir::FuncType Ty, llvm::StringRef Name,
874879
mlir::ArrayAttr = {}, bool Local = false,
875880
bool AssumeConvergent = false);

0 commit comments

Comments
 (0)