Skip to content

Commit 5805186

Browse files
committed
XXX
1 parent 9e47299 commit 5805186

26 files changed

+101
-87
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,10 +1192,6 @@ def CIR_OptNoneAttr : CIR_UnitAttr<"OptNone", "optnone"> {
11921192
let storageType = [{ OptNoneAttr }];
11931193
}
11941194

1195-
def CIR_NoThrowAttr : CIR_UnitAttr<"NoThrow", "nothrow"> {
1196-
let storageType = [{ NoThrowAttr }];
1197-
}
1198-
11991195
def CIR_ConvergentAttr : CIR_UnitAttr<"Convergent", "convergent"> {
12001196
let storageType = [{ ConvergentAttr }];
12011197
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4105,6 +4105,7 @@ def FuncOp : CIR_Op<"func", [
41054105
UnitAttr:$lambda,
41064106
UnitAttr:$no_proto,
41074107
UnitAttr:$dso_local,
4108+
UnitAttr:$nothrow,
41084109
DefaultValuedAttr<
41094110
CIR_GlobalLinkageKind, "GlobalLinkageKind::ExternalLinkage"
41104111
>:$linkage,

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ static void AddAttributesFromFunctionProtoType(CIRGenBuilderTy &builder,
162162

163163
if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) &&
164164
FPT->isNothrow()) {
165-
auto nu = cir::NoThrowAttr::get(builder.getContext());
166-
FuncAttrs.set(nu.getMnemonic(), nu);
165+
FuncAttrs.set("nothrow", builder.getUnitAttr());
167166
}
168167
}
169168

@@ -221,8 +220,8 @@ void CIRGenModule::constructAttributeList(
221220
if (TargetDecl) {
222221

223222
if (TargetDecl->hasAttr<NoThrowAttr>()) {
224-
auto nu = cir::NoThrowAttr::get(&getMLIRContext());
225-
funcAttrs.set(nu.getMnemonic(), nu);
223+
auto nu = mlir::UnitAttr::get(&getMLIRContext());
224+
funcAttrs.set("nothrow", nu);
226225
}
227226

228227
if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
@@ -324,7 +323,7 @@ emitCallLikeOp(CIRGenFunction &CGF, mlir::Location callLoc,
324323
cir::FuncOp directFuncOp,
325324
SmallVectorImpl<mlir::Value> &CIRCallArgs, bool isInvoke,
326325
cir::CallingConv callingConv, cir::SideEffect sideEffect,
327-
cir::ExtraFuncAttributesAttr extraFnAttrs) {
326+
cir::ExtraFuncAttributesAttr extraFnAttrs, bool cannotThrow) {
328327
auto &builder = CGF.getBuilder();
329328
auto getOrCreateSurroundingTryOp = [&]() {
330329
// In OG, we build the landing pad for this scope. In CIR, we emit a
@@ -383,6 +382,8 @@ emitCallLikeOp(CIRGenFunction &CGF, mlir::Location callLoc,
383382
callLoc, directFuncOp, CIRCallArgs, callingConv, sideEffect);
384383
}
385384
callOpWithExceptions->setAttr("extra_attrs", extraFnAttrs);
385+
if (cannotThrow)
386+
callOpWithExceptions->setAttr("nothrow", builder.getUnitAttr());
386387
CGF.mayThrow = true;
387388

388389
CGF.callWithExceptionCtx = callOpWithExceptions;
@@ -398,15 +399,20 @@ emitCallLikeOp(CIRGenFunction &CGF, mlir::Location callLoc,
398399
}
399400

400401
assert(builder.getInsertionBlock() && "expected valid basic block");
402+
cir::CIRCallOpInterface callOp;
401403
if (indirectFuncTy) {
402404
// TODO(cir): Set calling convention for indirect calls.
403405
assert(callingConv == cir::CallingConv::C && "NYI");
404-
return builder.createIndirectCallOp(
406+
callOp = builder.createIndirectCallOp(
405407
callLoc, indirectFuncVal, indirectFuncTy, CIRCallArgs,
406408
cir::CallingConv::C, sideEffect, extraFnAttrs);
409+
} else {
410+
callOp = builder.createCallOp(callLoc, directFuncOp, CIRCallArgs, callingConv,
411+
sideEffect, extraFnAttrs);
407412
}
408-
return builder.createCallOp(callLoc, directFuncOp, CIRCallArgs, callingConv,
409-
sideEffect, extraFnAttrs);
413+
if (cannotThrow)
414+
callOp->setAttr("nothrow", builder.getUnitAttr());
415+
return callOp;
410416
}
411417

412418
static RValue getRValueThroughMemory(mlir::Location loc,
@@ -601,12 +607,10 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &CallInfo,
601607
CannotThrow = true;
602608
} else {
603609
// Otherwise, nounwind call sites will never throw.
604-
auto noThrowAttr = cir::NoThrowAttr::get(&getMLIRContext());
605-
CannotThrow = Attrs.getNamed(noThrowAttr.getMnemonic()).has_value();
610+
CannotThrow = Attrs.getNamed("nothrow").has_value();
606611

607612
if (auto fptr = dyn_cast<cir::FuncOp>(CalleePtr))
608-
if (fptr.getExtraAttrs().getElements().contains(
609-
noThrowAttr.getMnemonic()))
613+
if (fptr.getNothrow())
610614
CannotThrow = true;
611615
}
612616
bool isInvoke = CannotThrow ? false : isInvokeDest();
@@ -650,7 +654,7 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &CallInfo,
650654

651655
cir::CIRCallOpInterface callLikeOp = emitCallLikeOp(
652656
*this, callLoc, indirectFuncTy, indirectFuncVal, directFuncOp,
653-
CIRCallArgs, isInvoke, callingConv, sideEffect, extraFnAttrs);
657+
CIRCallArgs, isInvoke, callingConv, sideEffect, extraFnAttrs, CannotThrow);
654658

655659
if (E)
656660
callLikeOp->setAttr("ast",
@@ -1462,8 +1466,8 @@ static void getTrivialDefaultFunctionAttributes(
14621466
llvm_unreachable("NYI");
14631467
if (langOpts.OpenCL ||
14641468
((langOpts.CUDA || langOpts.HIP) && langOpts.CUDAIsDevice)) {
1465-
auto noThrow = cir::NoThrowAttr::get(CGM.getBuilder().getContext());
1466-
funcAttrs.set(noThrow.getMnemonic(), noThrow);
1469+
auto noThrow = mlir::UnitAttr::get(CGM.getBuilder().getContext());
1470+
funcAttrs.set("nothrow", noThrow);
14671471
}
14681472
}
14691473

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,7 @@ static void tryMarkNoThrow(CIRGenFunction &cgf, cir::FuncOp fn) {
683683
if (isInterposable(fn) || cgf.mayThrow)
684684
return;
685685

686-
mlir::NamedAttrList extraAttrs{fn.getExtraAttrs().getElements().getValue()};
687-
auto noThrowAttr = cir::NoThrowAttr::get(&cgf.getMLIRContext());
688-
extraAttrs.set(noThrowAttr.getMnemonic(), noThrowAttr);
689-
fn.setExtraAttrsAttr(cir::ExtraFuncAttributesAttr::get(
690-
extraAttrs.getDictionary(&cgf.getMLIRContext())));
686+
fn.setNothrow(true);
691687
}
692688

693689
cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl gd, cir::FuncOp fn,

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,8 +2873,7 @@ void CIRGenModule::setCIRFunctionAttributesForDefinition(const Decl *decl,
28732873
llvm_unreachable("NYI");
28742874

28752875
if (!hasUnwindExceptions(getLangOpts())) {
2876-
auto attr = cir::NoThrowAttr::get(&getMLIRContext());
2877-
attrs.set(attr.getMnemonic(), attr);
2876+
f.setNothrow(true);
28782877
}
28792878

28802879
assert(!MissingFeatures::stackProtector());
@@ -3052,6 +3051,21 @@ void CIRGenModule::setCIRFunctionAttributes(GlobalDecl gd,
30523051
func.setExtraAttrsAttr(
30533052
cir::ExtraFuncAttributesAttr::get(pal.getDictionary(&getMLIRContext())));
30543053

3054+
// Set nothrow as a direct attribute if the function declaration has it or
3055+
// if the function type is nothrow.
3056+
const Decl *TargetDecl = gd.getDecl();
3057+
if (TargetDecl && TargetDecl->hasAttr<NoThrowAttr>()) {
3058+
func.setNothrow(true);
3059+
} else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
3060+
if (const FunctionProtoType *FPT =
3061+
Fn->getType()->getAs<FunctionProtoType>()) {
3062+
if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) &&
3063+
FPT->isNothrow()) {
3064+
func.setNothrow(true);
3065+
}
3066+
}
3067+
}
3068+
30553069
// TODO(cir): Check X86_VectorCall incompatibility with WinARM64EC
30563070

30573071
func.setCallingConv(callingConv);

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2641,6 +2641,7 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {
26412641
auto noProtoNameAttr = getNoProtoAttrName(state.name);
26422642
auto visibilityNameAttr = getGlobalVisibilityAttrName(state.name);
26432643
auto dsoLocalNameAttr = getDsoLocalAttrName(state.name);
2644+
auto nothrowNameAttr = getNothrowAttrName(state.name);
26442645
auto annotationsNameAttr = getAnnotationsAttrName(state.name);
26452646
auto cxxSpecialMemberAttr = getCxxSpecialMemberAttrName(state.name);
26462647
if (::mlir::succeeded(parser.parseOptionalKeyword(builtinNameAttr.strref())))
@@ -2676,6 +2677,8 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {
26762677

26772678
if (parser.parseOptionalKeyword(dsoLocalNameAttr).succeeded())
26782679
state.addAttribute(dsoLocalNameAttr, parser.getBuilder().getUnitAttr());
2680+
if (parser.parseOptionalKeyword(nothrowNameAttr).succeeded())
2681+
state.addAttribute(nothrowNameAttr, parser.getBuilder().getUnitAttr());
26792682

26802683
StringAttr nameAttr;
26812684
llvm::SmallVector<OpAsmParser::Argument, 8> arguments;
@@ -2907,6 +2910,9 @@ void cir::FuncOp::print(OpAsmPrinter &p) {
29072910
if (getDsoLocal())
29082911
p << " dso_local";
29092912

2913+
if (getNothrow())
2914+
p << " nothrow";
2915+
29102916
// Print function name, signature, and control.
29112917
p << ' ';
29122918
p.printSymbolName(getSymName());
@@ -2932,7 +2938,7 @@ void cir::FuncOp::print(OpAsmPrinter &p) {
29322938
p, *this,
29332939
// These are all omitted since they are custom printed already.
29342940
{getAliaseeAttrName(), getBuiltinAttrName(), getCoroutineAttrName(),
2935-
getDsoLocalAttrName(), getExtraAttrsAttrName(),
2941+
getDsoLocalAttrName(), getNothrowAttrName(), getExtraAttrsAttrName(),
29362942
getFunctionTypeAttrName(), getGlobalCtorPriorityAttrName(),
29372943
getGlobalDtorPriorityAttrName(), getLambdaAttrName(),
29382944
getLinkageAttrName(), getCallingConvAttrName(), getNoProtoAttrName(),

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,10 @@ mlir::LogicalResult CIRToLLVMFuncOpLowering::matchAndRewrite(
24932493
llvmFnTy, linkage, isDsoLocal, cconv,
24942494
mlir::SymbolRefAttr(), attributes);
24952495

2496+
if (op.getNothrow()) {
2497+
fn.setNoUnwind(true);
2498+
}
2499+
24962500
// Lower CIR attributes for arguments.
24972501
for (unsigned index = 0; index < fnType.getNumInputs(); index++) {
24982502
mlir::DictionaryAttr cirAttrs = op.getArgAttrDict(index);

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ class CIRDialectLLVMIRTranslationInterface
112112
llvm_unreachable("Unknown inline kind");
113113
} else if (mlir::dyn_cast<cir::OptNoneAttr>(attr.getValue())) {
114114
llvmFunc->addFnAttr(llvm::Attribute::OptimizeNone);
115-
} else if (mlir::dyn_cast<cir::NoThrowAttr>(attr.getValue())) {
116-
llvmFunc->addFnAttr(llvm::Attribute::NoUnwind);
117115
} else if (mlir::dyn_cast<cir::ConvergentAttr>(attr.getValue())) {
118116
llvmFunc->addFnAttr(llvm::Attribute::Convergent);
119117
} else if (mlir::isa<cir::HotAttr>(attr.getValue())) {

clang/test/CIR/CodeGen/OpenCL/nothrow.cl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55

66
// CIR-LABEL: #fn_attr =
77
// CIR: cl.kernel = #cir.cl.kernel
8-
// CIR: nothrow = #cir.nothrow
98

109
// CIR-LABEL: #fn_attr1 =
1110
// CIR-NOT: cl.kernel = #cir.cl.kernel
12-
// CIR: nothrow = #cir.nothrow
1311

1412
kernel void ker() {};
15-
// CIR: cir.func @ker{{.*}} extra(#fn_attr) {
13+
// CIR: cir.func nothrow @ker{{.*}} extra(#fn_attr) {
1614
// LLVM: define{{.*}}@ker(){{.*}} #0
1715

1816
void foo() {};
19-
// CIR: cir.func @foo{{.*}} extra(#fn_attr1) {
17+
// CIR: cir.func nothrow @foo{{.*}} extra(#fn_attr1) {
2018
// LLVM: define{{.*}}@foo(){{.*}} #1
2119

2220
// LLVM-LABEL: attributes #0

clang/test/CIR/CodeGen/call-extra-attrs.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ int s2(int a, int b) {
1818
return s1(a, b);
1919
}
2020

21-
// CIR: #fn_attr = #cir<extra({inline = #cir.inline<no>, nothrow = #cir.nothrow, optnone = #cir.optnone})>
22-
// CIR: #fn_attr1 = #cir<extra({nothrow = #cir.nothrow})>
21+
// CIR: #fn_attr = #cir<extra({inline = #cir.inline<no>, optnone = #cir.optnone})>
2322

2423
// CIR: cir.func {{.*}} @_Z2s0ii(%{{.*}}, %{{.*}}) -> {{.*}} extra(#fn_attr)
2524
// CIR: cir.func {{.*}} @_Z2s1ii(%{{.*}}, %{{.*}}) -> {{.*}} extra(#fn_attr)
26-
// CIR: cir.call @_Z2s0ii(%{{.*}}, %{{.*}}) : ({{.*}}, {{.*}}) -> {{.*}} extra(#fn_attr1)
2725
// CIR: cir.func {{.*}} @_Z2s2ii(%{{.*}}, %{{.*}}) -> {{.*}} extra(#fn_attr)
2826
// CHECK-NOT: cir.call @_Z2s1ii(%{{.*}}, %{{.*}}) : ({{.*}}, {{.*}}) -> {{.*}} extra(#fn_attr{{.*}})
2927

0 commit comments

Comments
 (0)