From e69f483316e9445508d0ef6f976446bde0c7574a Mon Sep 17 00:00:00 2001 From: Felix Daas Date: Fri, 16 May 2025 12:13:31 +0200 Subject: [PATCH 1/5] passing through sym_visibility, arg_attrs and res_attrs for cir.func to created func.func in cirtomlir lowering --- .../CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp index 7a0a9bceac23..d715a5a69695 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp +++ b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp @@ -540,13 +540,28 @@ class CIRFuncOpLowering : public mlir::OpConversionPattern { signatureConversion.addInputs(argType.index(), convertedType); } + SmallVector passThroughAttrs; + + if (auto symVisibilityAttr = op.getSymVisibilityAttr()) + passThroughAttrs.push_back( + rewriter.getNamedAttr("sym_visibility", symVisibilityAttr)); + + if (auto argAttrsAttr = op.getArgAttrsAttr()) + passThroughAttrs.push_back( + rewriter.getNamedAttr("arg_attrs", argAttrsAttr)); + + if (auto resAttrsAttr = op.getResAttrsAttr()) + passThroughAttrs.push_back( + rewriter.getNamedAttr("res_attrs", resAttrsAttr)); + mlir::Type resultType = getTypeConverter()->convertType(fnType.getReturnType()); auto fn = rewriter.create( op.getLoc(), op.getName(), rewriter.getFunctionType(signatureConversion.getConvertedTypes(), resultType ? mlir::TypeRange(resultType) - : mlir::TypeRange())); + : mlir::TypeRange()), + passThroughAttrs); if (failed(rewriter.convertRegionTypes(&op.getBody(), *typeConverter, &signatureConversion))) From 40cb63d321d441578373d423f44ac03fb33c202f Mon Sep 17 00:00:00 2001 From: Felix Daas Date: Fri, 16 May 2025 12:31:49 +0200 Subject: [PATCH 2/5] added coversion of CIR TargetTripleAttr to llvm mlir TargetTripleAttr when convertion MLIR core module to LLVM MLIR module + added removal of unnecessary CIR Attr --- .../lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp b/clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp index f46fe4533c2f..c30dfd788d0c 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp +++ b/clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp @@ -24,6 +24,7 @@ #include "mlir/Pass/PassManager.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" #include "mlir/Transforms/DialectConversion.h" +#include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/Passes.h" using namespace cir; @@ -62,6 +63,17 @@ void ConvertMLIRToLLVMPass::runOnOperation() { populateFuncToLLVMConversionPatterns(typeConverter, patterns); auto module = getOperation(); + + // Lower the module attributes to LLVM equivalents. + if (auto tripleAttr = module->getAttr(cir::CIRDialect::getTripleAttrName())) + module->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(), + tripleAttr); + + // Strip the CIR attributes. + module->removeAttr(cir::CIRDialect::getSOBAttrName()); + module->removeAttr(cir::CIRDialect::getLangAttrName()); + module->removeAttr(cir::CIRDialect::getTripleAttrName()); + if (failed(applyFullConversion(module, target, std::move(patterns)))) signalPassFailure(); } From 280bb46bcb4aec3ed9bbb1563964d8c2432c14bd Mon Sep 17 00:00:00 2001 From: Felix Daas Date: Wed, 21 May 2025 16:14:34 +0200 Subject: [PATCH 3/5] added two testcases to test if 1. the sym_visibility attr is passed correctly when a cir.func is lowered to a func.func in the indirect lowering (see: https: llvm/clangir#1618) 2. the cir.triple attr is converted to the llvm.triple attr correctly when compiling throughmlir (see: llvm/clangir#1619) --- .../CIR/Lowering/ThroughMLIR/compilation.c | 8 +++++ .../CIR/Lowering/ThroughMLIR/declaration.c | 29 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 clang/test/CIR/Lowering/ThroughMLIR/compilation.c create mode 100644 clang/test/CIR/Lowering/ThroughMLIR/declaration.c diff --git a/clang/test/CIR/Lowering/ThroughMLIR/compilation.c b/clang/test/CIR/Lowering/ThroughMLIR/compilation.c new file mode 100644 index 000000000000..04a052590bc4 --- /dev/null +++ b/clang/test/CIR/Lowering/ThroughMLIR/compilation.c @@ -0,0 +1,8 @@ +// RUN: %clang -fclangir -fno-clangir-direct-lowering %s -o %t + +int main(int argc, char *argv[]) { + // Variables + int number = 0; + + return number; +} \ No newline at end of file diff --git a/clang/test/CIR/Lowering/ThroughMLIR/declaration.c b/clang/test/CIR/Lowering/ThroughMLIR/declaration.c new file mode 100644 index 000000000000..4a34ab10f174 --- /dev/null +++ b/clang/test/CIR/Lowering/ThroughMLIR/declaration.c @@ -0,0 +1,29 @@ +// RUN: split-file %s %t +// RUN: %clang_cc1 -fclangir -fno-clangir-direct-lowering -emit-mlir=core %t%{fs-sep}test_declaration.c +// RUN: FileCheck --input-file=%t%{fs-sep}test_declaration.mlir %t%{fs-sep}test_declaration.c + + +//--- add10.h + +#ifndef ADD10_H +#define ADD10_H + +int add10(int x); + +#endif + + +//--- test_declaration.c + +#include "add10.h" + +// CHECK: func.func private @add10(i32) -> i32 + +int main(int argc, char *argv[]) { + // Variables + int number = 15; + + number = add10(number); + + return 0; +} \ No newline at end of file From 451ce562b5f5d56b87e81123f4969dd43ad04fdb Mon Sep 17 00:00:00 2001 From: Felix Daas Date: Thu, 22 May 2025 18:02:41 +0200 Subject: [PATCH 4/5] Simplified tests, added --triple option to declaration test + added constraint to compilation test to fix execution on windows and mac --- .../CIR/Lowering/ThroughMLIR/compilation.c | 13 ++++++++-- .../CIR/Lowering/ThroughMLIR/declaration.c | 25 +++++-------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/clang/test/CIR/Lowering/ThroughMLIR/compilation.c b/clang/test/CIR/Lowering/ThroughMLIR/compilation.c index 04a052590bc4..bbcee6ccae4e 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/compilation.c +++ b/clang/test/CIR/Lowering/ThroughMLIR/compilation.c @@ -1,6 +1,15 @@ -// RUN: %clang -fclangir -fno-clangir-direct-lowering %s -o %t +// Check that the `cir.triple` attribute is correctly translated into the LLVM `llvm.triple` attribute. +// This test cannot use `-triple x86_64-unknown-linux-gnu` explicitly, +// as the frontend must infer the triple based on the host. +// Restricting this test to supported targets until CIR supports more platforms. -int main(int argc, char *argv[]) { +// RUN: %clang -fclangir -fno-clangir-direct-lowering -c %s -o %t + +// TODO: Relax target constraint once CIR supports additional backends. +// REQUIRES: target=x86_64{{.*}}-linux{{.*}} + + +int test_target_triple_passthrough() { // Variables int number = 0; diff --git a/clang/test/CIR/Lowering/ThroughMLIR/declaration.c b/clang/test/CIR/Lowering/ThroughMLIR/declaration.c index 4a34ab10f174..dd3ddae44826 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/declaration.c +++ b/clang/test/CIR/Lowering/ThroughMLIR/declaration.c @@ -1,29 +1,16 @@ -// RUN: split-file %s %t -// RUN: %clang_cc1 -fclangir -fno-clangir-direct-lowering -emit-mlir=core %t%{fs-sep}test_declaration.c -// RUN: FileCheck --input-file=%t%{fs-sep}test_declaration.mlir %t%{fs-sep}test_declaration.c +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir +// RUN: FileCheck --input-file=%t.mlir %s -//--- add10.h +int declaration(int x); -#ifndef ADD10_H -#define ADD10_H +// CHECK: func.func private @declaration(i32) -> i32 -int add10(int x); - -#endif - - -//--- test_declaration.c - -#include "add10.h" - -// CHECK: func.func private @add10(i32) -> i32 - -int main(int argc, char *argv[]) { +int declaration_test() { // Variables int number = 15; - number = add10(number); + number = declaration(number); return 0; } \ No newline at end of file From 71a99b8ca365c20a9fdde0abf54d494be0621daa Mon Sep 17 00:00:00 2001 From: Felix Daas Date: Thu, 22 May 2025 23:23:39 +0200 Subject: [PATCH 5/5] removed the passing of arg_attrs and res_attrs + adjusted the tests --- .../CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp | 10 +--------- .../test/CIR/Lowering/ThroughMLIR/compilation.c | 17 ----------------- .../{declaration.c => function-attributes.c} | 11 ++--------- clang/test/CIR/driver.c | 1 + 4 files changed, 4 insertions(+), 35 deletions(-) delete mode 100644 clang/test/CIR/Lowering/ThroughMLIR/compilation.c rename clang/test/CIR/Lowering/ThroughMLIR/{declaration.c => function-attributes.c} (74%) diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp index d715a5a69695..74af213e9605 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp +++ b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp @@ -540,20 +540,12 @@ class CIRFuncOpLowering : public mlir::OpConversionPattern { signatureConversion.addInputs(argType.index(), convertedType); } - SmallVector passThroughAttrs; + SmallVector passThroughAttrs; if (auto symVisibilityAttr = op.getSymVisibilityAttr()) passThroughAttrs.push_back( rewriter.getNamedAttr("sym_visibility", symVisibilityAttr)); - if (auto argAttrsAttr = op.getArgAttrsAttr()) - passThroughAttrs.push_back( - rewriter.getNamedAttr("arg_attrs", argAttrsAttr)); - - if (auto resAttrsAttr = op.getResAttrsAttr()) - passThroughAttrs.push_back( - rewriter.getNamedAttr("res_attrs", resAttrsAttr)); - mlir::Type resultType = getTypeConverter()->convertType(fnType.getReturnType()); auto fn = rewriter.create( diff --git a/clang/test/CIR/Lowering/ThroughMLIR/compilation.c b/clang/test/CIR/Lowering/ThroughMLIR/compilation.c deleted file mode 100644 index bbcee6ccae4e..000000000000 --- a/clang/test/CIR/Lowering/ThroughMLIR/compilation.c +++ /dev/null @@ -1,17 +0,0 @@ -// Check that the `cir.triple` attribute is correctly translated into the LLVM `llvm.triple` attribute. -// This test cannot use `-triple x86_64-unknown-linux-gnu` explicitly, -// as the frontend must infer the triple based on the host. -// Restricting this test to supported targets until CIR supports more platforms. - -// RUN: %clang -fclangir -fno-clangir-direct-lowering -c %s -o %t - -// TODO: Relax target constraint once CIR supports additional backends. -// REQUIRES: target=x86_64{{.*}}-linux{{.*}} - - -int test_target_triple_passthrough() { - // Variables - int number = 0; - - return number; -} \ No newline at end of file diff --git a/clang/test/CIR/Lowering/ThroughMLIR/declaration.c b/clang/test/CIR/Lowering/ThroughMLIR/function-attributes.c similarity index 74% rename from clang/test/CIR/Lowering/ThroughMLIR/declaration.c rename to clang/test/CIR/Lowering/ThroughMLIR/function-attributes.c index dd3ddae44826..479e82180a47 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/declaration.c +++ b/clang/test/CIR/Lowering/ThroughMLIR/function-attributes.c @@ -1,16 +1,9 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s - -int declaration(int x); - // CHECK: func.func private @declaration(i32) -> i32 +int declaration(int x); int declaration_test() { - // Variables - int number = 15; - - number = declaration(number); - - return 0; + return declaration(15); } \ No newline at end of file diff --git a/clang/test/CIR/driver.c b/clang/test/CIR/driver.c index fcafb71a0a4a..b8e3d5634338 100644 --- a/clang/test/CIR/driver.c +++ b/clang/test/CIR/driver.c @@ -25,6 +25,7 @@ // RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR_MACOS // RUN: %clang -target arm64-apple-macosx12.0.0 -fclangir -S -emit-llvm %s -o %t3.ll // RUN: FileCheck --input-file=%t3.ll %s -check-prefix=LLVM_MACOS +// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -c %s -o %t void foo(void) {}