Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 8 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,20 @@ class CIRFuncOpLowering : public mlir::OpConversionPattern<cir::FuncOp> {
signatureConversion.addInputs(argType.index(), convertedType);
}

SmallVector<mlir::NamedAttribute, 2> 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<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
9 changes: 9 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/function-attributes.c
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions clang/test/CIR/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand Down
Loading