Skip to content

[CIR][Lowering] Core dialects: passthrough symbol visibility #1620

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 6 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,28 @@ class CIRFuncOpLowering : public mlir::OpConversionPattern<cir::FuncOp> {
signatureConversion.addInputs(argType.index(), convertedType);
}

SmallVector<mlir::NamedAttribute, 4> 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<mlir::func::FuncOp>(
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)))
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
17 changes: 17 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/compilation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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;
}
16 changes: 16 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/declaration.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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_test() {
// Variables
int number = 15;

number = declaration(number);

return 0;
}