Skip to content

Commit 18581fd

Browse files
committed
[CFE] Add nomerge function attribute to inline assembly.
Sometimes we also want to avoid merging inline assembly. This patch add the nomerge function attribute to inline assembly. Reviewed By: zequanwu Differential Revision: https://reviews.llvm.org/D84225
1 parent 0881d0b commit 18581fd

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,12 +1954,16 @@ static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
19541954
}
19551955

19561956
static void UpdateAsmCallInst(llvm::CallBase &Result, bool HasSideEffect,
1957-
bool ReadOnly, bool ReadNone, const AsmStmt &S,
1957+
bool ReadOnly, bool ReadNone, bool NoMerge,
1958+
const AsmStmt &S,
19581959
const std::vector<llvm::Type *> &ResultRegTypes,
19591960
CodeGenFunction &CGF,
19601961
std::vector<llvm::Value *> &RegResults) {
19611962
Result.addAttribute(llvm::AttributeList::FunctionIndex,
19621963
llvm::Attribute::NoUnwind);
1964+
if (NoMerge)
1965+
Result.addAttribute(llvm::AttributeList::FunctionIndex,
1966+
llvm::Attribute::NoMerge);
19631967
// Attach readnone and readonly attributes.
19641968
if (!HasSideEffect) {
19651969
if (ReadNone)
@@ -2334,12 +2338,14 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
23342338
Builder.CreateCallBr(IA, Fallthrough, Transfer, Args);
23352339
EmitBlock(Fallthrough);
23362340
UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, ReadOnly,
2337-
ReadNone, S, ResultRegTypes, *this, RegResults);
2341+
ReadNone, InNoMergeAttributedStmt, S, ResultRegTypes,
2342+
*this, RegResults);
23382343
} else {
23392344
llvm::CallInst *Result =
23402345
Builder.CreateCall(IA, Args, getBundlesForFunclet(IA));
23412346
UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, ReadOnly,
2342-
ReadNone, S, ResultRegTypes, *this, RegResults);
2347+
ReadNone, InNoMergeAttributedStmt, S, ResultRegTypes,
2348+
*this, RegResults);
23432349
}
23442350

23452351
assert(RegResults.size() == ResultRegTypes.size());

clang/lib/Sema/SemaStmtAttr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class CallExprFinder : public ConstEvaluatedExprVisitor<CallExprFinder> {
183183
bool foundCallExpr() { return FoundCallExpr; }
184184

185185
void VisitCallExpr(const CallExpr *E) { FoundCallExpr = true; }
186+
void VisitAsmStmt(const AsmStmt *S) { FoundCallExpr = true; }
186187

187188
void Visit(const Stmt *St) {
188189
if (!St)

clang/test/CodeGen/attr-nomerge.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ void foo(int i) {
1010
[[clang::nomerge]] f(bar(), bar());
1111
[[clang::nomerge]] [] { bar(); bar(); }(); // nomerge only applies to the anonymous function call
1212
[[clang::nomerge]] for (bar(); bar(); bar()) {}
13+
[[clang::nomerge]] { asm("nop"); }
1314
bar();
1415
}
1516
// CHECK: call zeroext i1 @_Z3barv() #[[NOMERGEATTR:[0-9]+]]
@@ -22,5 +23,7 @@ void foo(int i) {
2223
// CHECK: call zeroext i1 @_Z3barv() #[[NOMERGEATTR]]
2324
// CHECK: call zeroext i1 @_Z3barv() #[[NOMERGEATTR]]
2425
// CHECK: call zeroext i1 @_Z3barv() #[[NOMERGEATTR]]
26+
// CHECK: call void asm {{.*}} #[[NOMERGEATTR2:[0-9]+]]
2527
// CHECK: call zeroext i1 @_Z3barv()
2628
// CHECK: attributes #[[NOMERGEATTR]] = { nomerge }
29+
// CHECK: attributes #[[NOMERGEATTR2]] = { nomerge nounwind }

0 commit comments

Comments
 (0)