Skip to content
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

Problem with emitting MLIR from CIR: cir.func gets dsolocal attribute #727

Open
Moehre2 opened this issue Jul 9, 2024 · 4 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@Moehre2
Copy link

Moehre2 commented Jul 9, 2024

Hi, I am trying to use ClangIR to emit MLIR code from a C file. This is simple test that already fails (called test.c):

void foo(int i) {}

int main(void) {
    foo(2);
    return 0;
}

I built Clang from the current commit in the main branch (80e1a10). When I execute /work/wz273176/software/clangir/bin/clang -fclangir -Xclang -fno-clangir-direct-lowering -Xclang -emit-mlir -c test.c -o test.mlir, I get the following error:

clang: /work/wz273176/clangir/mlir/lib/IR/BuiltinAttributes.cpp:339: static mlir::FlatSymbolRefAttr mlir::SymbolRefAttr::get(mlir::Operation *): Assertion `symName && "value does not have a valid symbol name"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /work/wz273176/software/clangir/bin/clang -fclangir -Xclang -fno-clangir-direct-lowering -Xclang -emit-mlir -c test.c -o test.mlir
1.	<eof> parser at end of file
 #0 0x000055b18b025d07 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/work/wz273176/software/clangir/bin/clang+0x23c3d07)
 #1 0x000055b18b02386e llvm::sys::RunSignalHandlers() (/work/wz273176/software/clangir/bin/clang+0x23c186e)
 #2 0x000055b18b0250c3 llvm::sys::CleanupOnSignal(unsigned long) (/work/wz273176/software/clangir/bin/clang+0x23c30c3)
 #3 0x000055b18af9f4b8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #4 0x0000149ee056ad20 __restore_rt (/lib64/libpthread.so.0+0x12d20)
 #5 0x0000149edf58652f raise (/lib64/libc.so.6+0x4e52f)
 #6 0x0000149edf559e65 abort (/lib64/libc.so.6+0x21e65)
 #7 0x0000149edf559d39 _nl_load_domain.cold.0 (/lib64/libc.so.6+0x21d39)
 #8 0x0000149edf57ee86 (/lib64/libc.so.6+0x46e86)
 #9 0x000055b18d90a79f (/work/wz273176/software/clangir/bin/clang+0x4ca879f)
#10 0x000055b18c2e18a6 cir::CIRCallOpLowering::matchAndRewrite(mlir::cir::CallOp, mlir::cir::CallOpAdaptor, mlir::ConversionPatternRewriter&) const (/work/wz273176/software/clangir/bin/clang+0x367f8a6)
#11 0x000055b18c29f02a mlir::OpConversionPattern<mlir::cir::CallOp>::matchAndRewrite(mlir::Operation*, llvm::ArrayRef<mlir::Value>, mlir::ConversionPatternRewriter&) const (/work/wz273176/software/clangir/bin/clang+0x363d02a)
#12 0x000055b18d6d5adb mlir::ConversionPattern::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&) const (/work/wz273176/software/clangir/bin/clang+0x4a73adb)
#13 0x000055b18d713239 void llvm::function_ref<void ()>::callback_fn<mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<mlir::LogicalResult (mlir::Pattern const&)>)::$_2>(long) PatternApplicator.cpp:0:0
#14 0x000055b18d70fb3e mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<mlir::LogicalResult (mlir::Pattern const&)>) (/work/wz273176/software/clangir/bin/clang+0x4aadb3e)
#15 0x000055b18d6d692a (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#16 0x000055b18d6d5b77 mlir::OperationConverter::convert(mlir::ConversionPatternRewriter&, mlir::Operation*) (/work/wz273176/software/clangir/bin/clang+0x4a73b77)
#17 0x000055b18d6d6dcf mlir::OperationConverter::convertOperations(llvm::ArrayRef<mlir::Operation*>) (/work/wz273176/software/clangir/bin/clang+0x4a74dcf)
#18 0x000055b18d6decdb mlir::applyPartialConversion(mlir::Operation*, mlir::ConversionTarget const&, mlir::FrozenRewritePatternSet const&, mlir::ConversionConfig) (/work/wz273176/software/clangir/bin/clang+0x4a7ccdb)
#19 0x000055b18c2d73c1 cir::ConvertCIRToMLIRPass::runOnOperation() (/work/wz273176/software/clangir/bin/clang+0x36753c1)
#20 0x000055b18d80a706 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/work/wz273176/software/clangir/bin/clang+0x4ba8706)
#21 0x000055b18d80af21 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/work/wz273176/software/clangir/bin/clang+0x4ba8f21)
#22 0x000055b18d80d6a2 mlir::PassManager::run(mlir::Operation*) (/work/wz273176/software/clangir/bin/clang+0x4bab6a2)
#23 0x000055b18c2d7a0e cir::lowerFromCIRToMLIR(mlir::ModuleOp, mlir::MLIRContext*) (/work/wz273176/software/clangir/bin/clang+0x3675a0e)
#24 0x000055b18c2751d1 cir::CIRGenConsumer::HandleTranslationUnit(clang::ASTContext&) (/work/wz273176/software/clangir/bin/clang+0x36131d1)
#25 0x000055b18e3f03d7 clang::ParseAST(clang::Sema&, bool, bool) (/work/wz273176/software/clangir/bin/clang+0x578e3d7)
#26 0x000055b18ba60820 clang::FrontendAction::Execute() (/work/wz273176/software/clangir/bin/clang+0x2dfe820)
#27 0x000055b18b9ca19f clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/work/wz273176/software/clangir/bin/clang+0x2d6819f)
#28 0x000055b18bb4303f clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/work/wz273176/software/clangir/bin/clang+0x2ee103f)
#29 0x000055b189e82a49 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/work/wz273176/software/clangir/bin/clang+0x1220a49)
#30 0x000055b189e7f51d ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#31 0x000055b18b80a0a9 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::$_1>(long) Job.cpp:0:0
#32 0x000055b18af9f1fc llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/work/wz273176/software/clangir/bin/clang+0x233d1fc)
#33 0x000055b18b80960f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/work/wz273176/software/clangir/bin/clang+0x2ba760f)
#34 0x000055b18b7c2fbf clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/work/wz273176/software/clangir/bin/clang+0x2b60fbf)
#35 0x000055b18b7c3277 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/work/wz273176/software/clangir/bin/clang+0x2b61277)
#36 0x000055b18b7e41e7 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/work/wz273176/software/clangir/bin/clang+0x2b821e7)
#37 0x000055b189e7ea61 clang_main(int, char**, llvm::ToolContext const&) (/work/wz273176/software/clangir/bin/clang+0x121ca61)
#38 0x000055b189e8ea97 main (/work/wz273176/software/clangir/bin/clang+0x122ca97)
#39 0x0000149edf5727e5 __libc_start_main (/lib64/libc.so.6+0x3a7e5)
#40 0x000055b189e7ceae _start (/work/wz273176/software/clangir/bin/clang+0x121aeae)
clang: error: clang frontend command failed with exit code 134 (use -v to see invocation)
clang version 19.0.0git (https://github.com/llvm/clangir.git 80e1a10002fce76d0880ef15ff8f481dcad626c1)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /rwthfs/rz/cluster/work/wz273176/software/clangir/bin
Build config: +assertions
clang: error: unable to execute command: Aborted (core dumped)
clang: note: diagnostic msg: Error generating preprocessed source(s).

With /work/wz273176/software/clangir/bin/clang -fclangir -Xclang -emit-cir -c test.c -o test.cir I am able to emit the following CIR:

!s32i = !cir.int<s, 32>
#fn_attr = #cir<extra({inline = #cir.inline<no>, nothrow = #cir.nothrow, optnone = #cir.optnone})>
#loc3 = loc("test.c":1:10)
#loc4 = loc("test.c":1:14)
#loc13 = loc(fused[#loc3, #loc4])
module @"/rwthfs/rz/cluster/home/wz273176/test2/test.c" attributes {cir.lang = #cir.lang<c>, cir.sob = #cir.signed_overflow_behavior<undefined>, cir.triple = "x86_64-unknown-linux-gnu", dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<i64, dense<64> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr<272>, dense<64> : vector<4xi64>>, #dlti.dl_entry<i128, dense<128> : vector<2xi64>>, #dlti.dl_entry<f80, dense<128> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr, dense<64> : vector<4xi64>>, #dlti.dl_entry<i8, dense<8> : vector<2xi64>>, #dlti.dl_entry<i1, dense<8> : vector<2xi64>>, #dlti.dl_entry<i32, dense<32> : vector<2xi64>>, #dlti.dl_entry<i16, dense<16> : vector<2xi64>>, #dlti.dl_entry<f64, dense<64> : vector<2xi64>>, #dlti.dl_entry<f16, dense<16> : vector<2xi64>>, #dlti.dl_entry<!llvm.ptr<271>, dense<32> : vector<4xi64>>, #dlti.dl_entry<!llvm.ptr<270>, dense<32> : vector<4xi64>>, #dlti.dl_entry<f128, dense<128> : vector<2xi64>>, #dlti.dl_entry<"dlti.stack_alignment", 128 : i64>, #dlti.dl_entry<"dlti.endianness", "little">>, llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"} {
  cir.func dsolocal @foo(%arg0: !s32i loc(fused[#loc3, #loc4])) extra(#fn_attr) {
    %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init] {alignment = 4 : i64} loc(#loc13)
    cir.store %arg0, %0 : !s32i, !cir.ptr<!s32i> loc(#loc5)
    cir.return loc(#loc2)
  } loc(#loc12)
  cir.func dsolocal @main() -> !s32i extra(#fn_attr) {
    %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"] {alignment = 4 : i64} loc(#loc7)
    %1 = cir.const #cir.int<2> : !s32i loc(#loc8)
    cir.call @foo(%1) : (!s32i) -> () loc(#loc9)
    %2 = cir.const #cir.int<0> : !s32i loc(#loc10)
    cir.store %2, %0 : !s32i, !cir.ptr<!s32i> loc(#loc15)
    %3 = cir.load %0 : !cir.ptr<!s32i>, !s32i loc(#loc15)
    cir.return %3 : !s32i loc(#loc15)
  } loc(#loc14)
} loc(#loc)
#loc = loc("/rwthfs/rz/cluster/home/wz273176/test2/test.c":0:0)
#loc1 = loc("test.c":1:1)
#loc2 = loc("test.c":1:18)
#loc5 = loc("test.c":1:17)
#loc6 = loc("test.c":3:1)
#loc7 = loc("test.c":6:1)
#loc8 = loc("test.c":4:9)
#loc9 = loc("test.c":4:5)
#loc10 = loc("test.c":5:12)
#loc11 = loc("test.c":5:5)
#loc12 = loc(fused[#loc1, #loc2])
#loc14 = loc(fused[#loc6, #loc7])
#loc15 = loc(fused[#loc11, #loc10])

When I now try to lower to MLIR with /work/wz273176/software/clangir/bin/cir-opt -o test.mlir test.cir, I get the following error:

test.cir:7:12: error: custom op 'cir.func' expected valid '@'-identifier for symbol name
  cir.func dsolocal @foo(%arg0: !s32i loc(fused[#loc3, #loc4])) extra(#fn_attr) {
           ^

Since I cannot find dsolocal in the documentation for cir.func but only in the documentation for cir.global, my guess would be that it is mistakenly added to cir.func. Any help is appreciated!

Moritz

@bcardosolopes
Copy link
Member

@ghehg looks like you missed some bits for parser support? Updated docs also seem to be lacking. Can you take a look?

@Lancern
Copy link
Collaborator

Lancern commented Jul 10, 2024

The crash dump should be irrelevant of the potential parser bug. I believe this is due to a bug in the implementation of CIRCallOpLowering in the MLIR lowering path:

mlir::LogicalResult
matchAndRewrite(mlir::cir::CallOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
SmallVector<mlir::Type> types;
if (mlir::failed(
getTypeConverter()->convertTypes(op.getResultTypes(), types)))
return mlir::failure();
rewriter.replaceOpWithNewOp<mlir::func::CallOp>(
op, mlir::SymbolRefAttr::get(op), types, adaptor.getOperands());
return mlir::LogicalResult::success();
}

On line 102, the second parameter to replaceOpWithNewOp should be op.getCalleeAttr(). I changed it locally to:

rewriter.replaceOpWithNewOp<mlir::func::CallOp>(
  op, op.getCalleeAttr(), types, adaptor.getOperands());

and then it works fine.

@Lancern Lancern added the bug Something isn't working label Jul 10, 2024
Lancern added a commit to Lancern/clangir that referenced this issue Jul 10, 2024
This patch should resolve the crash reported in llvm#727 .
@Lancern
Copy link
Collaborator

Lancern commented Jul 10, 2024

Opened a PR #728 for resolving the crash dump. The parser bug is not resolved.

@ghehg
Copy link
Collaborator

ghehg commented Jul 10, 2024

@ghehg looks like you missed some bits for parser support? Updated docs also seem to be lacking. Can you take a look?

sure, looking at it now.

bcardosolopes pushed a commit that referenced this issue Jul 10, 2024
This PR fixes the bug described as in
#727 (comment). It
should resolve the crash reported in #727 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants