Skip to content

Commit

Permalink
[CFE] Add nomerge function attribute to inline assembly.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
phoebewang committed Jul 22, 2020
1 parent 0881d0b commit 18581fd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions clang/lib/CodeGen/CGStmt.cpp
Expand Up @@ -1954,12 +1954,16 @@ static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
}

static void UpdateAsmCallInst(llvm::CallBase &Result, bool HasSideEffect,
bool ReadOnly, bool ReadNone, const AsmStmt &S,
bool ReadOnly, bool ReadNone, bool NoMerge,
const AsmStmt &S,
const std::vector<llvm::Type *> &ResultRegTypes,
CodeGenFunction &CGF,
std::vector<llvm::Value *> &RegResults) {
Result.addAttribute(llvm::AttributeList::FunctionIndex,
llvm::Attribute::NoUnwind);
if (NoMerge)
Result.addAttribute(llvm::AttributeList::FunctionIndex,
llvm::Attribute::NoMerge);
// Attach readnone and readonly attributes.
if (!HasSideEffect) {
if (ReadNone)
Expand Down Expand Up @@ -2334,12 +2338,14 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
Builder.CreateCallBr(IA, Fallthrough, Transfer, Args);
EmitBlock(Fallthrough);
UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, ReadOnly,
ReadNone, S, ResultRegTypes, *this, RegResults);
ReadNone, InNoMergeAttributedStmt, S, ResultRegTypes,
*this, RegResults);
} else {
llvm::CallInst *Result =
Builder.CreateCall(IA, Args, getBundlesForFunclet(IA));
UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, ReadOnly,
ReadNone, S, ResultRegTypes, *this, RegResults);
ReadNone, InNoMergeAttributedStmt, S, ResultRegTypes,
*this, RegResults);
}

assert(RegResults.size() == ResultRegTypes.size());
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Sema/SemaStmtAttr.cpp
Expand Up @@ -183,6 +183,7 @@ class CallExprFinder : public ConstEvaluatedExprVisitor<CallExprFinder> {
bool foundCallExpr() { return FoundCallExpr; }

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

void Visit(const Stmt *St) {
if (!St)
Expand Down
3 changes: 3 additions & 0 deletions clang/test/CodeGen/attr-nomerge.cpp
Expand Up @@ -10,6 +10,7 @@ void foo(int i) {
[[clang::nomerge]] f(bar(), bar());
[[clang::nomerge]] [] { bar(); bar(); }(); // nomerge only applies to the anonymous function call
[[clang::nomerge]] for (bar(); bar(); bar()) {}
[[clang::nomerge]] { asm("nop"); }
bar();
}
// CHECK: call zeroext i1 @_Z3barv() #[[NOMERGEATTR:[0-9]+]]
Expand All @@ -22,5 +23,7 @@ void foo(int i) {
// CHECK: call zeroext i1 @_Z3barv() #[[NOMERGEATTR]]
// CHECK: call zeroext i1 @_Z3barv() #[[NOMERGEATTR]]
// CHECK: call zeroext i1 @_Z3barv() #[[NOMERGEATTR]]
// CHECK: call void asm {{.*}} #[[NOMERGEATTR2:[0-9]+]]
// CHECK: call zeroext i1 @_Z3barv()
// CHECK: attributes #[[NOMERGEATTR]] = { nomerge }
// CHECK: attributes #[[NOMERGEATTR2]] = { nomerge nounwind }

0 comments on commit 18581fd

Please sign in to comment.