如何在 LLVM IR 的 switch 指令中使用 char*?
Posted
技术标签:
【中文标题】如何在 LLVM IR 的 switch 指令中使用 char*?【英文标题】:How to use char* in switch instruction of LLVM IR? 【发布时间】:2020-08-14 13:38:30 【问题描述】:我正在尝试在 switch 指令中使用字符串。我的想法是使用字符串的地址作为字符串的值(ConstantInt*)传递给switch指令,如果两个地址相等,switch case
应该匹配。
但是当我在 IR 代码(由我的程序生成)下运行时,会发生错误:
source_filename = "input/step/step1.ts"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin19.6.0"
@aa = private unnamed_addr constant [3 x i8] c"aa\00", align 1
@b = linkonce_odr global i8* getelementptr inbounds ([3 x i8], [3 x i8]* @aa, i32 0, i32 0)
@bb = private unnamed_addr constant [3 x i8] c"bb\00", align 1
define i32 @main()
%1 = load i8*, i8** @b
switch i8* %1, label %4 [
i8* getelementptr inbounds ([3 x i8], [3 x i8]* @aa, i32 0, i32 0), label %2
i8* getelementptr inbounds ([3 x i8], [3 x i8]* @bb, i32 0, i32 0), label %3
]
2: ; preds = %0
br label %4
3: ; preds = %0
br label %4
4: ; preds = %2, %3, %0
ret i32 0
语言代码如下:
var b: string = "aa";
switch (b)
case "aa":
break;
case "bb":
break;
IR 代码似乎是正确的,但运行时出现错误:
Stack dump:
0. Program arguments: ./build/tser input/step/step1.ts
1. Running pass 'Function Pass Manager' on module 'input/step/step1.ts'.
2. Running pass 'X86 DAG->DAG Instruction Selection' on function '@main'
0 tser 0x00000001073a718c llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 60
1 tser 0x00000001073a7749 PrintStackTraceSignalHandler(void*) + 25
2 tser 0x00000001073a52b6 llvm::sys::RunSignalHandlers() + 118
3 tser 0x00000001073ab21c SignalHandler(int) + 252
4 libsystem_platform.dylib 0x00007fff6efa65fd _sigtramp + 29
5 libsystem_platform.dylib 0x00007ffee8c53700 _sigtramp + 18446744071457919264
6 tser 0x0000000107d22fad llvm::APInt::operator[](unsigned int) const + 141
7 tser 0x0000000107d22e8d llvm::APInt::isNegative() const + 29
8 tser 0x00000001071d2d92 llvm::APInt::compareSigned(llvm::APInt const&) const + 242
9 tser 0x0000000107d2814d llvm::APInt::slt(llvm::APInt const&) const + 29
10 tser 0x0000000108dda1ee llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1::operator()(llvm::SwitchCG::CaseCluster const&, llvm::SwitchCG::CaseCluster const&) const + 62
11 tser 0x0000000108dd9b53 void std::__1::__sort<llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1&, llvm::SwitchCG::CaseCluster*>(llvm::SwitchCG::CaseCluster*, llvm::SwitchCG::CaseCluster*, llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1&) + 147
12 tser 0x0000000108dd9ab5 void std::__1::sort<llvm::SwitchCG::CaseCluster*, llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1&>(llvm::SwitchCG::CaseCluster*, llvm::SwitchCG::CaseCluster*, llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1&) + 37
13 tser 0x0000000108dd9a86 void std::__1::sort<llvm::SwitchCG::CaseCluster, llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1>(std::__1::__wrap_iter<llvm::SwitchCG::CaseCluster*>, std::__1::__wrap_iter<llvm::SwitchCG::CaseCluster*>, llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1) + 54
14 tser 0x0000000108dd99dd void llvm::sort<std::__1::__wrap_iter<llvm::SwitchCG::CaseCluster*>, llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1>(std::__1::__wrap_iter<llvm::SwitchCG::CaseCluster*>, std::__1::__wrap_iter<llvm::SwitchCG::CaseCluster*>, llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1) + 45
15 tser 0x0000000108dd06f3 void llvm::sort<std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&, llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1>(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&, llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&)::$_1) + 51
16 tser 0x0000000108dd0491 llvm::SwitchCG::sortAndRangeify(std::__1::vector<llvm::SwitchCG::CaseCluster, std::__1::allocator<llvm::SwitchCG::CaseCluster> >&) + 225
17 tser 0x00000001084fe009 llvm::SelectionDAGBuilder::visitSwitch(llvm::SwitchInst const&) + 569
18 tser 0x00000001084fb508 llvm::SelectionDAGBuilder::visit(unsigned int, llvm::User const&) + 152
19 tser 0x00000001084fac9d llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) + 141
20 tser 0x00000001086002cc llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, true>, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, true>, bool&) + 220
21 tser 0x00000001085ffcbe llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) + 6110
22 tser 0x00000001085fc9f2 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 2130
23 tser 0x0000000107a281d5 (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 277
24 tser 0x00000001089ebee6 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 550
25 tser 0x00000001075cb369 llvm::FPPassManager::runOnFunction(llvm::Function&) + 681
26 tser 0x00000001075cb945 llvm::FPPassManager::runOnModule(llvm::Module&) + 117
27 tser 0x00000001075cc0a8 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) + 776
28 tser 0x00000001075cbbd5 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 293
29 tser 0x00000001075cc6d1 llvm::legacy::PassManager::run(llvm::Module&) + 33
30 tser 0x0000000107775275 llvm::orc::SimpleCompiler::operator()(llvm::Module&) + 309
31 tser 0x000000010786ed64 decltype(std::__1::forward<llvm::orc::TMOwningSimpleCompiler&>(fp)(std::__1::forward<llvm::Module&>(fp0))) std::__1::__invoke<llvm::orc::TMOwningSimpleCompiler&, llvm::Module&>(llvm::orc::TMOwningSimpleCompiler&, llvm::Module&) + 68
32 tser 0x000000010786ecd4 llvm::Expected<std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> > > std::__1::__invoke_void_return_wrapper<llvm::Expected<std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> > > >::__call<llvm::orc::TMOwningSimpleCompiler&, llvm::Module&>(llvm::orc::TMOwningSimpleCompiler&, llvm::Module&) + 68
33 tser 0x000000010786ec84 std::__1::__function::__alloc_func<llvm::orc::TMOwningSimpleCompiler, std::__1::allocator<llvm::orc::TMOwningSimpleCompiler>, llvm::Expected<std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> > > (llvm::Module&)>::operator()(llvm::Module&) + 68
34 tser 0x000000010786daf3 std::__1::__function::__func<llvm::orc::TMOwningSimpleCompiler, std::__1::allocator<llvm::orc::TMOwningSimpleCompiler>, llvm::Expected<std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> > > (llvm::Module&)>::operator()(llvm::Module&) + 67
35 tser 0x000000010781d127 std::__1::__function::__value_func<llvm::Expected<std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> > > (llvm::Module&)>::operator()(llvm::Module&) const + 103
36 tser 0x000000010781d08f std::__1::function<llvm::Expected<std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> > > (llvm::Module&)>::operator()(llvm::Module&) const + 63
37 tser 0x000000010781bb26 decltype(fp(decltype(std::__1::__declval<llvm::Module&>(0)) std::__1::declval<llvm::Module&>()())) llvm::orc::ThreadSafeModule::withModuleDo<std::__1::function<llvm::Expected<std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer> > > (llvm::Module&)>&>(llvm::Module&) + 182
38 tser 0x000000010781b84b llvm::orc::IRCompileLayer::emit(llvm::orc::MaterializationResponsibility, llvm::orc::ThreadSafeModule) + 187
39 tser 0x0000000107847cc6 llvm::orc::BasicIRLayerMaterializationUnit::materialize(llvm::orc::MaterializationResponsibility) + 470
40 tser 0x0000000107873280 llvm::orc::MaterializationUnit::doMaterialize(llvm::orc::JITDylib&) + 112
41 tser 0x0000000107786dc0 llvm::orc::ExecutionSession::materializeOnCurrentThread(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >) + 32
42 tser 0x00000001077931a4 decltype(std::__1::forward<void (*&)(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >)>(fp)(std::__1::forward<llvm::orc::JITDylib&>(fp0), std::__1::forward<std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> > >(fp0))) std::__1::__invoke<void (*&)(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >), llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> > >(void (*&)(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >), llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >&&) + 84
43 tser 0x0000000107793127 void std::__1::__invoke_void_return_wrapper<void>::__call<void (*&)(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >), llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> > >(void (*&)(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >), llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >&&) + 71
44 tser 0x00000001077930d7 std::__1::__function::__alloc_func<void (*)(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >), std::__1::allocator<void (*)(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >)>, void (llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >)>::operator()(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >&&) + 71
45 tser 0x0000000107791ec6 std::__1::__function::__func<void (*)(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >), std::__1::allocator<void (*)(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >)>, void (llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >)>::operator()(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >&&) + 70
46 tser 0x000000010778d01a std::__1::__function::__value_func<void (llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >)>::operator()(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >&&) const + 106
47 tser 0x000000010778cec5 std::__1::function<void (llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >)>::operator()(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >) const + 69
48 tser 0x0000000107783143 llvm::orc::ExecutionSession::dispatchMaterialization(llvm::orc::JITDylib&, std::__1::unique_ptr<llvm::orc::MaterializationUnit, std::__1::default_delete<llvm::orc::MaterializationUnit> >) + 163
49 tser 0x0000000107784c50 llvm::orc::ExecutionSession::runOutstandingMUs() + 320
50 tser 0x0000000107781f4c llvm::orc::ExecutionSession::lookup(llvm::orc::LookupKind, std::__1::vector<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>, std::__1::allocator<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags> > > const&, llvm::orc::SymbolLookupSet, llvm::orc::SymbolState, llvm::unique_function<void (llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::JITEvaluatedSymbol, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::JITEvaluatedSymbol> > >)>, std::__1::function<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> >, llvm::DenseMapInfo<llvm::orc::JITDylib*>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > > > const&)>) + 892
51 tser 0x0000000107787b34 llvm::orc::ExecutionSession::lookup(std::__1::vector<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>, std::__1::allocator<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags> > > const&, llvm::orc::SymbolLookupSet const&, llvm::orc::LookupKind, llvm::orc::SymbolState, std::__1::function<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> >, llvm::DenseMapInfo<llvm::orc::JITDylib*>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > > > const&)>) + 324
52 tser 0x0000000107787dc3 llvm::orc::ExecutionSession::lookup(std::__1::vector<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>, std::__1::allocator<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags> > > const&, llvm::orc::SymbolStringPtr) + 307
53 tser 0x00000001078533fd llvm::orc::LLJIT::lookupLinkerMangled(llvm::orc::JITDylib&, llvm::StringRef) + 173
54 tser 0x0000000106fcdcf9 llvm::orc::LLJIT::lookup(llvm::orc::JITDylib&, llvm::StringRef) + 169
55 tser 0x0000000106fc1acb llvm::orc::LLJIT::lookup(llvm::StringRef) + 75
56 tser 0x0000000106fc0888 main + 2632
57 libdyld.dylib 0x00007fff6edadcc9 start + 1
58 libdyld.dylib 0x0000000000000002 start + 18446603338656326458
make: *** [start] Segmentation fault: 11
没有任何教程,我找不到它,非常感谢任何人的帮助。
【问题讨论】:
您不能使用字符串,因为它们的值是一个地址,并且为时已晚。只有在编译器将开关转换为一系列机器代码指令之后,才在链接时分配。 【参考方案1】:您将不得不阅读 llvm 文档:https://llvm.org/docs/LangRef.html#switch-instruction。请注意,在 switch
语句中使用 strings
是不正确的。它必须在switch
语句的参数中是一个整数,这样llvm 字节码才能正常工作。
您必须将给定的switch
语句(如您向我们展示的那样)转换为与llvm 字节码中的switch
等效的if..else-if..else
,因为将字符串直接传递到switch
语句中不会像您预期的那样执行。当 switch
表达式不是 int
时,将 switch
转换为等效的 if..else-if..else
并且它应该可以工作。
【讨论】:
我终于找到了另一种方法,我认为它比纯if-else
实现更好。它改为使用案例索引,并结合 switch 指令和if-else
指令。你可以看看我的回答:***.com/a/63453227/11354404.【参考方案2】:
最后我找到了一个更好的方法,我认为它比纯 if-else 实现更好。它改用index of cases
,并结合了switch指令和if-else指令:
-
将case的字符串值转换为case的索引,类型为ConstantInt。
计算case值的匹配索引,这一步使用if-else,在
switch
指令之前运行。
然后使用switch
指令(索引为键)。
如以下代码:
var a: number = 11;
var b: string = "aa";
switch (b)
case "aa":
a = a + 100;
break;
case "bb":
a = a + 200;
break;
将其转换为以下 IR 代码:
@a = linkonce_odr global i32 11
@aa = private unnamed_addr constant [3 x i8] c"aa\00", align 1
@bb = private unnamed_addr constant [3 x i8] c"bb\00", align 1
@b = linkonce_odr global i8* getelementptr inbounds ([3 x i8], [3 x i8]* @aa, i32 0, i32 0)
define i32 @main()
%1 = alloca i32
%2 = load i8*, i8** @b
%3 = icmp eq i8* %2, getelementptr inbounds ([3 x i8], [3 x i8]* @aa, i32 0, i32 0)
br i1 %3, label %4, label %5
4: ; preds = %0
store i32 0, i32* %1
br label %5
5: ; preds = %4, %0
%6 = load i8*, i8** @b
%7 = icmp eq i8* %6, getelementptr inbounds ([3 x i8], [3 x i8]* @bb, i32 0, i32 0)
br i1 %7, label %8, label %9
8: ; preds = %5
store i32 1, i32* %1
br label %9
9: ; preds = %8, %5
%10 = load i32, i32* %1
switch i32 %10, label %17 [
i32 0, label %11
i32 1, label %14
]
11: ; preds = %9
%12 = load i32, i32* @a
%13 = add i32 %12, 100
store i32 %13, i32* @a
br label %17
14: ; preds = %9
%15 = load i32, i32* @a
%16 = add i32 %15, 200
store i32 %16, i32* @a
br label %17
17: ; preds = %9, %11, %14
ret i32 0
【讨论】:
以上是关于如何在 LLVM IR 的 switch 指令中使用 char*?的主要内容,如果未能解决你的问题,请参考以下文章
LLVM IR:具有可变 BasicBlock 目标的分支指令?