Skip to content

Commit 95bef37

Browse files
felixdaaslanza
authored andcommitted
[CIR][Lowering] Core dialects: passthrough symbol visibility (llvm#1620)
Fixes llvm#1619 Fixes llvm#1618
1 parent e028ab7 commit 95bef37

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ class CIRFuncOpLowering : public mlir::OpConversionPattern<cir::FuncOp> {
544544
signatureConversion.addInputs(argType.index(), convertedType);
545545
}
546546

547+
SmallVector<mlir::NamedAttribute, 2> passThroughAttrs;
548+
549+
if (auto symVisibilityAttr = op.getSymVisibilityAttr())
550+
passThroughAttrs.push_back(
551+
rewriter.getNamedAttr("sym_visibility", symVisibilityAttr));
552+
547553
SmallVector<mlir::Type> resultTypes;
548554
// Only convert return type if the function is not void
549555
if (!mlir::isa<cir::VoidType>(fnType.getReturnType())) {
@@ -556,7 +562,8 @@ class CIRFuncOpLowering : public mlir::OpConversionPattern<cir::FuncOp> {
556562
auto fn = rewriter.create<mlir::func::FuncOp>(
557563
op.getLoc(), op.getName(),
558564
rewriter.getFunctionType(signatureConversion.getConvertedTypes(),
559-
resultTypes));
565+
resultTypes),
566+
passThroughAttrs);
560567

561568
if (failed(rewriter.convertRegionTypes(&op.getBody(), *getTypeConverter(),
562569
&signatureConversion)))

clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "mlir/Pass/PassManager.h"
2525
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
2626
#include "mlir/Transforms/DialectConversion.h"
27+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
2728
#include "clang/CIR/Passes.h"
2829

2930
using namespace cir;
@@ -62,6 +63,17 @@ void ConvertMLIRToLLVMPass::runOnOperation() {
6263
populateFuncToLLVMConversionPatterns(typeConverter, patterns);
6364

6465
auto module = getOperation();
66+
67+
// Lower the module attributes to LLVM equivalents.
68+
if (auto tripleAttr = module->getAttr(cir::CIRDialect::getTripleAttrName()))
69+
module->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(),
70+
tripleAttr);
71+
72+
// Strip the CIR attributes.
73+
module->removeAttr(cir::CIRDialect::getSOBAttrName());
74+
module->removeAttr(cir::CIRDialect::getLangAttrName());
75+
module->removeAttr(cir::CIRDialect::getTripleAttrName());
76+
6577
if (failed(applyFullConversion(module, target, std::move(patterns))))
6678
signalPassFailure();
6779
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir=core %s -o %t.mlir
2+
// RUN: FileCheck --input-file=%t.mlir %s
3+
4+
// CHECK: func.func private @declaration(i32) -> i32
5+
6+
int declaration(int x);
7+
int declaration_test() {
8+
return declaration(15);
9+
}

clang/test/CIR/driver.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR_MACOS
2626
// RUN: %clang -target arm64-apple-macosx12.0.0 -fclangir -S -emit-llvm %s -o %t3.ll
2727
// RUN: FileCheck --input-file=%t3.ll %s -check-prefix=LLVM_MACOS
28+
// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -c %s -o %t.o
2829

2930
void foo(void) {}
3031

0 commit comments

Comments
 (0)