diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp index 7a0a9bceac23..74af213e9605 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp +++ b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp @@ -540,13 +540,20 @@ 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)); + 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))) 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(); } diff --git a/clang/test/CIR/Lowering/ThroughMLIR/function-attributes.c b/clang/test/CIR/Lowering/ThroughMLIR/function-attributes.c new file mode 100644 index 000000000000..e8a196e65005 --- /dev/null +++ b/clang/test/CIR/Lowering/ThroughMLIR/function-attributes.c @@ -0,0 +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 + +// CHECK: func.func private @declaration(i32) -> i32 + +int declaration(int x); +int declaration_test() { + return declaration(15); +} diff --git a/clang/test/CIR/driver.c b/clang/test/CIR/driver.c index fcafb71a0a4a..f06db665ebbd 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.o void foo(void) {}