-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[CIR] Upstream builtin lowering emitter & FAbs op #151750
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// 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 -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll | ||
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll | ||
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG | ||
|
||
double fabs(double x) { | ||
return __builtin_fabs(x); | ||
} | ||
|
||
// CIR: {{.*}} = cir.fabs {{.*}} : !cir.double | ||
// LLVM: {{.*}} = call double @llvm.fabs.f64(double {{.*}}) | ||
// OGCG: {{.*}} = call double @llvm.fabs.f64(double {{.*}}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// RUN: clang-tblgen -gen-cir-builtins-lowering -I%S %s -o - 2>&1 | \ | ||
// RUN: FileCheck --strict-whitespace %s | ||
// REQUIRES: clangir | ||
|
||
def ins; | ||
def outs; | ||
|
||
class OpBase { | ||
dag arguments = (ins); | ||
dag results = (outs); | ||
} | ||
|
||
class LLVMLoweringInfo { | ||
string llvmOp = ""; | ||
} | ||
|
||
class CIR_Op : LLVMLoweringInfo, OpBase; | ||
|
||
def CIR_FAbsOp : CIR_Op { | ||
let arguments = (ins); | ||
let results = (outs); | ||
|
||
let llvmOp = "FAbsOp"; | ||
} | ||
|
||
// CHECK: #ifdef GET_BUILTIN_LOWERING_CLASSES_DECLARE | ||
// CHECK: class CIRFAbsOpLowering : public mlir::OpConversionPattern<cir::FAbsOp> { | ||
// CHECK: public: | ||
// CHECK: using OpConversionPattern<cir::FAbsOp>::OpConversionPattern; | ||
// CHECK: mlir::LogicalResult | ||
// CHECK: matchAndRewrite(cir::FAbsOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const override; | ||
// CHECK: }; | ||
// CHECK: #endif | ||
// CHECK: #ifdef GET_BUILTIN_LOWERING_CLASSES_DEF | ||
// CHECK: mlir::LogicalResult | ||
// CHECK: CIRFAbsOpLowering::matchAndRewrite(cir::FAbsOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { | ||
// CHECK: rewriter.replaceOpWithNewOp<mlir::LLVM::FAbsOp>(op); | ||
// CHECK: return mlir::success(); | ||
// CHECK: } | ||
// CHECK: #endif | ||
// CHECK: #ifdef GET_BUILTIN_LOWERING_LIST | ||
// CHECK: , CIRFAbsOpLowering | ||
// CHECK: #endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
//===- CIRLoweringEmitter.cpp - Generate lowering of builtins --=-*- C++ -*--=// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "TableGenBackends.h" | ||
#include "llvm/TableGen/TableGenBackend.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
std::string ClassDeclaration; | ||
std::string ClassDefinitions; | ||
std::string ClassList; | ||
|
||
// Adapted from mlir/lib/TableGen/Operator.cpp | ||
// Returns the C++ class name of the operation, which is the name of the | ||
// operation with the dialect prefix removed and the first underscore removed. | ||
// If the operation name starts with an underscore, the underscore is considered | ||
// part of the class name. | ||
std::string getCppClassName(const Record *Operation) { | ||
StringRef Name = Operation->getName(); | ||
auto [prefix, cppClassName] = Name.split('_'); | ||
if (prefix.empty()) { | ||
// Class name with a leading underscore and without dialect prefix | ||
return Name.str(); | ||
} | ||
|
||
if (cppClassName.empty()) { | ||
// Class name without dialect prefix | ||
return prefix.str(); | ||
} | ||
|
||
return cppClassName.str(); | ||
} | ||
|
||
void GenerateLowering(const Record *Operation) { | ||
using namespace std::string_literals; | ||
std::string Name = getCppClassName(Operation); | ||
std::string LLVMOp = Operation->getValueAsString("llvmOp").str(); | ||
|
||
ClassDeclaration += | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it even necessary to use tablegen for this? Could we not have a single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be possiable yes, i will try it :D There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can't create lowering for the table gen |
||
"class CIR" + Name + | ||
"Lowering : public mlir::OpConversionPattern<cir::" + Name + | ||
R"C++(> { | ||
public: | ||
using OpConversionPattern<cir::)C++" + | ||
Name + R"C++(>::OpConversionPattern; | ||
|
||
mlir::LogicalResult | ||
matchAndRewrite(cir::)C++" + | ||
Name + | ||
" op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) " | ||
"const " | ||
"override;" + | ||
R"C++( | ||
}; | ||
)C++"; | ||
|
||
ClassDefinitions += | ||
R"C++(mlir::LogicalResult | ||
CIR)C++" + | ||
Name + "Lowering::matchAndRewrite(cir::" + Name + | ||
R"C++( op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const {)C++"; | ||
|
||
auto ResultCount = Operation->getValueAsDag("results")->getNumArgs(); | ||
if (ResultCount > 0) | ||
ClassDefinitions += R"C++( | ||
auto resTy = this->getTypeConverter()->convertType(op.getType());)C++"; | ||
|
||
ClassDefinitions += R"C++( | ||
rewriter.replaceOpWithNewOp<mlir::LLVM::)C++" + | ||
LLVMOp + ">(op"; | ||
|
||
if (ResultCount > 0) | ||
ClassDefinitions += ", resTy"; | ||
|
||
size_t ArgCount = Operation->getValueAsDag("arguments")->getNumArgs(); | ||
for (size_t i = 0; i != ArgCount; ++i) | ||
ClassDefinitions += ", adaptor.getOperands()[" + std::to_string(i) + ']'; | ||
|
||
ClassDefinitions += R"C++(); | ||
return mlir::success(); | ||
} | ||
)C++"; | ||
|
||
ClassList += ", CIR" + Name + "Lowering\n"; | ||
} | ||
} // namespace | ||
|
||
void clang::EmitCIRBuiltinsLowering(const RecordKeeper &Records, | ||
raw_ostream &OS) { | ||
emitSourceFileHeader("Lowering of ClangIR builtins to LLVM IR builtins", OS); | ||
for (const auto *Builtin : | ||
Records.getAllDerivedDefinitions("LLVMLoweringInfo")) { | ||
if (!Builtin->getValueAsString("llvmOp").empty()) | ||
GenerateLowering(Builtin); | ||
} | ||
|
||
OS << "#ifdef GET_BUILTIN_LOWERING_CLASSES_DECLARE\n" | ||
<< ClassDeclaration << "\n#endif\n"; | ||
OS << "#ifdef GET_BUILTIN_LOWERING_CLASSES_DEF\n" | ||
<< ClassDefinitions << "\n#endif\n"; | ||
OS << "#ifdef GET_BUILTIN_LOWERING_LIST\n" << ClassList << "\n#endif\n"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
|
||
#include "ASTTableGen.h" | ||
#include "TableGenBackends.h" // Declares all backends. | ||
#include "clang/Config/config.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/ManagedStatic.h" | ||
#include "llvm/Support/PrettyStackTrace.h" | ||
|
@@ -25,6 +26,9 @@ using namespace clang; | |
enum ActionType { | ||
PrintRecords, | ||
DumpJSON, | ||
#if CLANG_ENABLE_CIR | ||
GenCIRBuiltinsLowering, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of the changes in this directory need to be guarded by a |
||
#endif // CLANG_ENABLE_CIR | ||
GenClangAttrClasses, | ||
GenClangAttrParserStringSwitches, | ||
GenClangAttrSubjectMatchRulesParserStringSwitches, | ||
|
@@ -128,6 +132,11 @@ cl::opt<ActionType> Action( | |
"Print all records to stdout (default)"), | ||
clEnumValN(DumpJSON, "dump-json", | ||
"Dump all records as machine-readable JSON"), | ||
#if CLANG_ENABLE_CIR | ||
clEnumValN(GenCIRBuiltinsLowering, "gen-cir-builtins-lowering", | ||
"Generate lowering of ClangIR builtins to equivalent LLVM " | ||
"IR builtins"), | ||
#endif // CLANG_ENABLE_CIR | ||
clEnumValN(GenClangAttrClasses, "gen-clang-attr-classes", | ||
"Generate clang attribute clases"), | ||
clEnumValN(GenClangAttrParserStringSwitches, | ||
|
@@ -354,6 +363,11 @@ bool ClangTableGenMain(raw_ostream &OS, const RecordKeeper &Records) { | |
case DumpJSON: | ||
EmitJSON(Records, OS); | ||
break; | ||
#if CLANG_ENABLE_CIR | ||
case GenCIRBuiltinsLowering: | ||
EmitCIRBuiltinsLowering(Records, OS); | ||
break; | ||
#endif // CLANG_ENABLE_CIR | ||
case GenClangAttrClasses: | ||
EmitClangAttrClass(Records, OS); | ||
break; | ||
|
@@ -639,7 +653,7 @@ bool ClangTableGenMain(raw_ostream &OS, const RecordKeeper &Records) { | |
|
||
return false; | ||
} | ||
} | ||
} // namespace | ||
|
||
int main(int argc, char **argv) { | ||
sys::PrintStackTraceOnErrorSignal(argv[0]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for the tablegen handling in this file? There are some examples in
clang/test/TableGen/
of clang-tablegen testing in general. What is the minimum .td file needed to make this work?