-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[CIR] Implement codegen for inline assembly without input and output operands #153546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
//===--- CIRGenAsm.cpp - Inline Assembly Support for CIR CodeGen ---------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file contains code to emit inline assembly. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/Basic/DiagnosticSema.h" | ||
#include "llvm/ADT/StringExtras.h" | ||
|
||
#include "CIRGenFunction.h" | ||
#include "TargetInfo.h" | ||
#include "clang/CIR/MissingFeatures.h" | ||
|
||
using namespace clang; | ||
using namespace clang::CIRGen; | ||
using namespace cir; | ||
|
||
static AsmFlavor inferFlavor(const CIRGenModule &cgm, const AsmStmt &s) { | ||
AsmFlavor gnuAsmFlavor = | ||
cgm.getCodeGenOpts().getInlineAsmDialect() == CodeGenOptions::IAD_ATT | ||
? AsmFlavor::x86_att | ||
: AsmFlavor::x86_intel; | ||
|
||
return isa<MSAsmStmt>(&s) ? AsmFlavor::x86_intel : gnuAsmFlavor; | ||
} | ||
|
||
static void collectClobbers(const CIRGenFunction &cgf, const AsmStmt &s, | ||
std::string &constraints, bool &hasUnwindClobber, | ||
bool &readOnly, bool readNone) { | ||
|
||
hasUnwindClobber = false; | ||
const CIRGenModule &cgm = cgf.getCIRGenModule(); | ||
|
||
// Clobbers | ||
for (unsigned i = 0, e = s.getNumClobbers(); i != e; i++) { | ||
std::string clobberStr = s.getClobber(i); | ||
StringRef clobber{clobberStr}; | ||
if (clobber == "memory") | ||
el-ev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
readOnly = readNone = false; | ||
else if (clobber == "unwind") { | ||
hasUnwindClobber = true; | ||
continue; | ||
} else if (clobber != "cc") { | ||
clobber = cgf.getTarget().getNormalizedGCCRegisterName(clobber); | ||
if (cgm.getCodeGenOpts().StackClashProtector && | ||
cgf.getTarget().isSPRegName(clobber)) { | ||
cgm.getDiags().Report(s.getAsmLoc(), | ||
diag::warn_stack_clash_protection_inline_asm); | ||
} | ||
} | ||
|
||
if (isa<MSAsmStmt>(&s)) { | ||
if (clobber == "eax" || clobber == "edx") { | ||
if (constraints.find("=&A") != std::string::npos) | ||
continue; | ||
std::string::size_type position1 = | ||
constraints.find("={" + clobber.str() + "}"); | ||
if (position1 != std::string::npos) { | ||
constraints.insert(position1 + 1, "&"); | ||
continue; | ||
} | ||
std::string::size_type position2 = constraints.find("=A"); | ||
if (position2 != std::string::npos) { | ||
constraints.insert(position2 + 1, "&"); | ||
continue; | ||
} | ||
} | ||
} | ||
if (!constraints.empty()) | ||
constraints += ','; | ||
|
||
constraints += "~{"; | ||
constraints += clobber; | ||
constraints += '}'; | ||
} | ||
|
||
// Add machine specific clobbers | ||
std::string_view machineClobbers = cgf.getTarget().getClobbers(); | ||
if (!machineClobbers.empty()) { | ||
if (!constraints.empty()) | ||
constraints += ','; | ||
constraints += machineClobbers; | ||
} | ||
} | ||
|
||
mlir::LogicalResult CIRGenFunction::emitAsmStmt(const AsmStmt &s) { | ||
// Assemble the final asm string. | ||
std::string asmString = s.generateAsmString(getContext()); | ||
|
||
std::string constraints; | ||
|
||
// An inline asm can be marked readonly if it meets the following conditions: | ||
// - it doesn't have any sideeffects | ||
// - it doesn't clobber memory | ||
// - it doesn't return a value by-reference | ||
// It can be marked readnone if it doesn't have any input memory constraints | ||
// in addition to meeting the conditions listed above. | ||
bool readOnly = true, readNone = true; | ||
|
||
if (s.getNumInputs() != 0 || s.getNumOutputs() != 0) { | ||
assert(!cir::MissingFeatures::asmInputOperands()); | ||
assert(!cir::MissingFeatures::asmOutputOperands()); | ||
cgm.errorNYI(s.getAsmLoc(), "asm with operands"); | ||
} | ||
|
||
bool hasUnwindClobber = false; | ||
collectClobbers(*this, s, constraints, hasUnwindClobber, readOnly, readNone); | ||
|
||
llvm::SmallVector<mlir::ValueRange, 8> operands; | ||
mlir::Type resultType; | ||
|
||
bool hasSideEffect = s.isVolatile() || s.getNumOutputs() == 0; | ||
|
||
cir::InlineAsmOp ia = builder.create<cir::InlineAsmOp>( | ||
getLoc(s.getAsmLoc()), resultType, operands, asmString, constraints, | ||
hasSideEffect, inferFlavor(cgm, s), mlir::ArrayAttr()); | ||
|
||
if (false /*IsGCCAsmGoto*/) { | ||
el-ev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
assert(!cir::MissingFeatures::asmGoto()); | ||
} else if (hasUnwindClobber) { | ||
assert(!cir::MissingFeatures::asmUnwindClobber()); | ||
} else { | ||
assert(!cir::MissingFeatures::asmMemoryEffects()); | ||
} | ||
|
||
llvm::SmallVector<mlir::Attribute> operandAttrs; | ||
ia.setOperandAttrsAttr(builder.getArrayAttr(operandAttrs)); | ||
|
||
return mlir::success(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir | ||
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll | ||
// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM | ||
|
||
void f1() { | ||
// CIR: cir.asm(x86_att, | ||
// CIR: {"" "~{dirflag},~{fpsr},~{flags}"}) side_effects | ||
// LLVM: call void asm sideeffect "", "~{dirflag},~{fpsr},~{flags}"() | ||
__asm__ volatile("" : : : ); | ||
} | ||
|
||
void f2() { | ||
// CIR: cir.asm(x86_att, | ||
// CIR: {"nop" "~{dirflag},~{fpsr},~{flags}"}) side_effects | ||
// LLVM: call void asm sideeffect "nop", "~{dirflag},~{fpsr},~{flags}"() | ||
__asm__ volatile("nop" : : : ); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.