[llvm-dev] How to change the metadata at an IR Node

Nitish Srivastava via llvm-dev llvm-dev at lists.llvm.org
Tue Sep 5 13:26:36 PDT 2017


Hi,

I am trying to change the debugLoc atached to an IR node in the Loop
Strength Reduction pass. I have the following code:

llvm::MDString* name = MDString::get(Ctx, "foo" );

llvm::MDNode *node = llvm::MDNode::get(Ctx, name);

I->setDebugLoc(DILocation::get(Ctx,I->getDebugLoc().getLine(),I->getDebugLoc().getCol(),node,
nullptr));  // here I is the IR instruction I want to tag with "foo"
metadata

However, when I run the opt tool, I get the following error:

        clang-3.9:
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/include/llvm/Support/Casting.h:238:
typename llvm::cast_retty<X, Y*>::ret_type llvm::cast(Y*) [with X =
llvm::DILocalScope; Y = llvm::Metadata; typename llvm::cast_retty<X,
Y*>::ret_type = llvm::DILocalScope*]: Assertion `isa<X>(Val) &&
"cast<Ty>() argument of incompatible type!"' failed.
#0 0x0000000002034e94 llvm::sys::PrintStackTrace(llvm::raw_ostream&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/Support/Unix/Signals.inc:402:0
#1 0x0000000002035258 PrintStackTraceSignalHandler(void*)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/Support/Unix/Signals.inc:470:0
#2 0x000000000203356c llvm::sys::RunSignalHandlers()
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/Support/Signals.cpp:44:0
#3 0x00000000020347e0 SignalHandler(int)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/Support/Unix/Signals.inc:256:0
#4 0x00000039f120f7e0 __restore_rt (/lib64/libpthread.so.0+0x39f120f7e0)
#5 0x00000039f0632495 __GI_raise (/lib64/libc.so.6+0x39f0632495)
#6 0x00000039f0633c75 __GI_abort (/lib64/libc.so.6+0x39f0633c75)
#7 0x00000039f062b60e __assert_fail_base (/lib64/libc.so.6+0x39f062b60e)
#8 0x00000039f062b6d0 __GI___assert_perror_fail (/lib64/libc.so.6+0x39f062b6d0)
#9 0x0000000001763b63 llvm::cast_retty<llvm::DILocalScope,
llvm::Metadata*>::ret_type llvm::cast<llvm::DILocalScope,
llvm::Metadata>(llvm::Metadata*)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/include/llvm/Support/Casting.h:240:0
#10 0x00000000017632fe llvm::DILocation::getScope() const
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/include/llvm/IR/DebugInfoMetadata.h:1235:0
#11 0x0000000001ae53b3 llvm::DILocation::getInlinedAtScope() const
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/include/llvm/IR/DebugInfoMetadata.h:1252:0
#12 0x0000000001bbaaaa (anonymous
namespace)::Verifier::visitFunction(llvm::Function const&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/Verifier.cpp:2069:0
#13 0x0000000001bc8a18 llvm::InstVisitor<(anonymous
namespace)::Verifier, void>::visit(llvm::Function&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/include/llvm/IR/InstVisitor.h:103:0
#14 0x0000000001bafd6d (anonymous
namespace)::Verifier::verify(llvm::Function const&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/Verifier.cpp:303:0
#15 0x0000000001bc8541 (anonymous
namespace)::VerifierLegacyPass::runOnFunction(llvm::Function&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/Verifier.cpp:4332:0
#16 0x0000000001b439ab
llvm::FPPassManager::runOnFunction(llvm::Function&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/LegacyPassManager.cpp:1526:0
#17 0x0000000001b43b22 llvm::FPPassManager::runOnModule(llvm::Module&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/LegacyPassManager.cpp:1547:0
#18 0x0000000001b43e6f (anonymous
namespace)::MPPassManager::runOnModule(llvm::Module&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/LegacyPassManager.cpp:1603:0
#19 0x0000000001b44521
llvm::legacy::PassManagerImpl::run(llvm::Module&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/LegacyPassManager.cpp:1706:0
#20 0x0000000001b4472d llvm::legacy::PassManager::run(llvm::Module&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/LegacyPassManager.cpp:1738:0
#21 0x0000000002269a96 (anonymous
namespace)::EmitAssemblyHelper::EmitAssembly(clang::BackendAction,
llvm::raw_pwrite_stream*)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/CodeGen/BackendUtil.cpp:765:0
#22 0x0000000002269b6f
clang::EmitBackendOutput(clang::DiagnosticsEngine&,
clang::CodeGenOptions const&, clang::TargetOptions const&,
clang::LangOptions const&, llvm::DataLayout const&, llvm::Module*,
clang::BackendAction, llvm::raw_pwrite_stream*)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/CodeGen/BackendUtil.cpp:782:0
#23 0x0000000002bb809d
clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/CodeGen/CodeGenAction.cpp:180:0
#24 0x00000000036247a7 clang::ParseAST(clang::Sema&, bool, bool)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/Parse/ParseAST.cpp:169:0
#25 0x00000000027932d5 clang::ASTFrontendAction::ExecuteAction()
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/Frontend/FrontendAction.cpp:557:0
#26 0x0000000002bb71df clang::CodeGenAction::ExecuteAction()
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/CodeGen/CodeGenAction.cpp:851:0
#27 0x0000000002792d7b clang::FrontendAction::Execute()
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/Frontend/FrontendAction.cpp:461:0
#28 0x00000000027433d4
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/Frontend/CompilerInstance.cpp:877:0
#29 0x00000000028ae585
clang::ExecuteCompilerInvocation(clang::CompilerInstance*)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:241:0
#30 0x000000000148d854 cc1_main(llvm::ArrayRef<char const*>, char
const*, void*) /work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/tools/driver/cc1_main.cpp:116:0
#31 0x0000000001483b89 ExecuteCC1Tool(llvm::ArrayRef<char const*>,
llvm::StringRef)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/tools/driver/driver.cpp:301:0
#32 0x0000000001484614 main
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/tools/driver/driver.cpp:382:0
#33 0x00000039f061ed1d __libc_start_main (/lib64/libc.so.6+0x39f061ed1d)
#34 0x00000000014814e1 _start
(/work/zhang/users/nks45/RISCV-LLVM/bin/clang-3.9+0x14814e1)
Stack dump:
0.    Program arguments:
/work/zhang/users/nks45/RISCV-LLVM/bin/clang-3.9 -cc1 -triple riscv64
-S -disable-free -main-file-name vvadd-xloops.c -mrelocation-model
static -mthread-model posix -mdisable-fp-elim -fmath-errno
-no-integrated-as -mconstructor-aliases -target-feature +rv64
-target-feature +a -target-feature +m -target-feature +f
-target-feature +d -dwarf-column-info -debug-info-kind=limited
-dwarf-version=4 -debugger-tuning=gdb -coverage-file
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tests/vvadd-xloops.c
-resource-dir /work/zhang/users/nks45/RISCV-LLVM/bin/../lib/clang/3.9.0
-dependency-file vvadd-xloops.d -MT vvadd-xloops.o -MP -I .
-I/work/zhang/users/nks45/python3/include/python3.6m -I. -O3
-fno-dwarf-directory-asm -fdebug-compilation-dir
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tests -ferror-limit 19
-fmessage-length 157 -fobjc-runtime=gcc -fdiagnostics-show-option
-fcolor-diagnostics -vectorize-loops -vectorize-slp -o vvadd-xloops.s
-x c vvadd-xloops.c
1.    <eof> parser at end of file
2.    Code generation
3.    Running pass 'Function Pass Manager' on module 'vvadd-xloops.c'.
4.    Running pass 'Module Verifier' on function '@vvadd_xloops'
clang-3.9: error: unable to execute command: Aborted
clang-3.9: error: clang frontend command failed due to signal (use -v
to see invocation)
clang version 3.9.0 (https://github.com/riscv/riscv-clang.git
52e04ed86397148b5cac4c54c27713168d9f1c5e)
(git at github.com:nitish2112/riscv-llvm.git
6bb1a18fd05a39f3bffcc5fc4bf5d5b527dca995)
Target: riscv64

Thread model: posix

>From the error message and stack trace it can be seen that there is an
assertion failure while casting in Verifier.cpp. Is thisthe right way to
change the debug info attached to an instruction? I found this post from
2015 which was also handling the same issue (
https://groups.google.com/forum/#!topic/llvm-dev/Yn5x83GycZE ). According
to this post, the second argument has to be the scope. When I try this code
instead:

llvm::MDString* name = MDString::get(Ctx, "foo" );

llvm::MDNode *node = llvm::MDNode::get(Ctx, name);
I->setDebugLoc(DILocation::get(Ctx,I->getDebugLoc().getLine(),I->getDebugLoc().getCol(),
,I->getDebugLoc().getScope(), node));  // here I is the IR instruction
I want to tag with "foo" metadata

This gives me following error:

        #0 0x0000000002034e9c
llvm::sys::PrintStackTrace(llvm::raw_ostream&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/Support/Unix/Signals.inc:402:0
#1 0x0000000002035260 PrintStackTraceSignalHandler(void*)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/Support/Unix/Signals.inc:470:0
#2 0x0000000002033574 llvm::sys::RunSignalHandlers()
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/Support/Signals.cpp:44:0
#3 0x00000000020347e8 SignalHandler(int)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/Support/Unix/Signals.inc:256:0
#4 0x00000039f120f7e0 __restore_rt (/lib64/libpthread.so.0+0x39f120f7e0)
#5 0x00000039f0632495 __GI_raise (/lib64/libc.so.6+0x39f0632495)
#6 0x00000039f0633c75 __GI_abort (/lib64/libc.so.6+0x39f0633c75)
#7 0x00000039f062b60e __assert_fail_base (/lib64/libc.so.6+0x39f062b60e)
#8 0x00000039f062b6d0 __GI___assert_perror_fail (/lib64/libc.so.6+0x39f062b6d0)
#9 0x0000000001763bbf llvm::cast_retty<llvm::DILocation,
llvm::Metadata*>::ret_type llvm::cast_or_null<llvm::DILocation,
llvm::Metadata>(llvm::Metadata*)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/include/llvm/Support/Casting.h:271:0
#10 0x0000000001763328 llvm::DILocation::getInlinedAt() const
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/include/llvm/IR/DebugInfoMetadata.h:1238:0
#11 0x0000000001ae538e llvm::DILocation::getInlinedAtScope() const
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/include/llvm/IR/DebugInfoMetadata.h:1249:0
#12 0x0000000001bbaaaa (anonymous
namespace)::Verifier::visitFunction(llvm::Function const&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/Verifier.cpp:2069:0
#13 0x0000000001bc8a18 llvm::InstVisitor<(anonymous
namespace)::Verifier, void>::visit(llvm::Function&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/include/llvm/IR/InstVisitor.h:103:0
#14 0x0000000001bafd6d (anonymous
namespace)::Verifier::verify(llvm::Function const&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/Verifier.cpp:303:0
#15 0x0000000001bc8541 (anonymous
namespace)::VerifierLegacyPass::runOnFunction(llvm::Function&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/Verifier.cpp:4332:0
#16 0x0000000001b439ab
llvm::FPPassManager::runOnFunction(llvm::Function&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/LegacyPassManager.cpp:1526:0
#17 0x0000000001b43b22 llvm::FPPassManager::runOnModule(llvm::Module&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/LegacyPassManager.cpp:1547:0
#18 0x0000000001b43e6f (anonymous
namespace)::MPPassManager::runOnModule(llvm::Module&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/LegacyPassManager.cpp:1603:0
#19 0x0000000001b44521
llvm::legacy::PassManagerImpl::run(llvm::Module&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/LegacyPassManager.cpp:1706:0
#20 0x0000000001b4472d llvm::legacy::PassManager::run(llvm::Module&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/lib/IR/LegacyPassManager.cpp:1738:0
#21 0x0000000002269a9e (anonymous
namespace)::EmitAssemblyHelper::EmitAssembly(clang::BackendAction,
llvm::raw_pwrite_stream*)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/CodeGen/BackendUtil.cpp:765:0
#22 0x0000000002269b77
clang::EmitBackendOutput(clang::DiagnosticsEngine&,
clang::CodeGenOptions const&, clang::TargetOptions const&,
clang::LangOptions const&, llvm::DataLayout const&, llvm::Module*,
clang::BackendAction, llvm::raw_pwrite_stream*)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/CodeGen/BackendUtil.cpp:782:0
#23 0x0000000002bb80a5
clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/CodeGen/CodeGenAction.cpp:180:0#24
0x00000000036247af clang::ParseAST(clang::Sema&, bool, bool)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/Parse/ParseAST.cpp:169:0#25
0x00000000027932dd clang::ASTFrontendAction::ExecuteAction()
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/Frontend/FrontendAction.cpp:557:0#26
0x0000000002bb71e7 clang::CodeGenAction::ExecuteAction()
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/CodeGen/CodeGenAction.cpp:851:0#27
0x0000000002792d83 clang::FrontendAction::Execute()
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/Frontend/FrontendAction.cpp:461:0#28
0x00000000027433dc
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/Frontend/CompilerInstance.cpp:877:0#29
0x00000000028ae58d
clang::ExecuteCompilerInvocation(clang::CompilerInstance*)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:241:0#30
0x000000000148d854 cc1_main(llvm::ArrayRef<char const*>, char const*,
void*) /work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/tools/driver/cc1_main.cpp:116:0#31
0x0000000001483b89 ExecuteCC1Tool(llvm::ArrayRef<char const*>,
llvm::StringRef)
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/tools/driver/driver.cpp:301:0#32
0x0000000001484614 main
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tools/clang/tools/driver/driver.cpp:382:0#33
0x00000039f061ed1d __libc_start_main (/lib64/libc.so.6+0x39f061ed1d)
#34 0x00000000014814e1 _start
(/work/zhang/users/nks45/RISCV-LLVM/bin/clang-3.9+0x14814e1)
Stack dump:
0.      Program arguments:
/work/zhang/users/nks45/RISCV-LLVM/bin/clang-3.9 -cc1 -triple riscv64
-S -disable-free -main-file-name vvadd-xloops.c -mrelocation-model
static -mthread-model posix -mdisable-fp-elim -fmath-errno
-no-integrated-as -mconstructor-aliases -target-feature +rv64
-target-feature +a -target-feature +m -target-feature +f
-target-feature +d -dwarf-column-info -debug-info-kind=limited
-dwarf-version=4 -debugger-tuning=gdb -coverage-file
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tests/vvadd-xloops.c
-resource-dir /work/zhang/users/nks45/RISCV-LLVM/bin/../lib/clang/3.9.0
-dependency-file vvadd-xloops.d -MT vvadd-xloops.o -MP -I .
-I/work/zhang/users/nks45/python3/include/python3.6m -I. -O3
-fno-dwarf-directory-asm -fdebug-compilation-dir
/work/zhang/users/nks45/RISCV-LLVM/riscv-llvm/tests -ferror-limit 19
-fmessage-length 0 -fobjc-runtime=gcc -fdiagnostics-show-option
-vectorize-loops -vectorize-slp -o vvadd-xloops.s -x c vvadd-xloops.c

Can someone tell what is the right way of doing this?

Thanks,

Best Regards,
Nitish
‚Äč
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170905/cc3004fb/attachment-0001.html>


More information about the llvm-dev mailing list