How to get #variabe_name from getelementptr inbounds instruction - llvm

I read the documentation but not sure how to fetch this value. I was able to read direct getelementptr instructions.
Code:
struct foo {
unsigned char position;
};
extern struct foo f1;
void hello () {
if (f1.position){
}
}
IR code:
%struct.foo = type { i8 }
#f1 = external dso_local global %struct.foo, align 1
; Function Attrs: noinline nounwind optnone uwtable mustprogress
define dso_local void #_hello() #0 !dbg !9 {
%1 = load i8, i8* getelementptr inbounds (%struct.foo, %struct.foo* #f1, i32 0, i32 0), align 1, !dbg !13
%2 = icmp ne i8 %1, 0, !dbg !15
br i1 %2, label %3, label %4, !dbg !16
3:
br label %4, !dbg !17
4:
ret void, !dbg !19
}
I would like to fetch f1 from getelementptr inbounds instruction.
Many Thanks.

Related

LLVM asserts "Resolving symbol with incorrect flags"

I'm following the example of Kaleidoscope to write a minimum IR file interpreter. It takes one command line argument, which is a path to .ll file, and executes the main function in the file. But when I tested it on an IR file, it failed with:
Assertion failed: (KV.second.getFlags() & ~WeakFlags) == (I->second & ~WeakFlags) && "Resolving symbol with incorrect flags", file <path>\llvm\lib\ExecutionEngine\Orc\Core.cpp, line 2775
Considering the simplicity of my code (which only has 63 lines), I can't figure out what's wrong in it. Please help 😭!!!
Full Source Code
#include <iostream>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/ExecutionEngine/Orc/CompileUtils.h>
#include <llvm/ExecutionEngine/Orc/Core.h>
#include <llvm/ExecutionEngine/Orc/ExecutionUtils.h>
#include <llvm/ExecutionEngine/Orc/ExecutorProcessControl.h>
#include <llvm/ExecutionEngine/Orc/IRCompileLayer.h>
#include <llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h>
#include <llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
using namespace llvm;
int
main(int argc, char* argv[])
{
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
orc::ThreadSafeContext tsctx(std::make_unique<LLVMContext>());
SMDiagnostic error;
auto mod = parseIRFile(argv[1], error, *tsctx.getContext());
auto epc = orc::SelfExecutorProcessControl::Create();
cantFail(epc.takeError());
orc::ExecutionSession es(std::move(*epc));
auto triple = es.getExecutorProcessControl().getTargetTriple();
orc::JITTargetMachineBuilder jtmb(triple);
auto dl = jtmb.getDefaultDataLayoutForTarget();
cantFail(dl.takeError());
orc::RTDyldObjectLinkingLayer ol(
es, []() { return std::make_unique<SectionMemoryManager>(); });
orc::IRCompileLayer cl(
es, ol, std::make_unique<orc::ConcurrentIRCompiler>(std::move(jtmb)));
auto& jd = es.createBareJITDylib("jd");
jd.addGenerator(
cantFail(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
dl->getGlobalPrefix())));
cantFail(cl.add(jd, orc::ThreadSafeModule(std::move(mod), tsctx)));
orc::MangleAndInterner mangle(es, *dl);
auto f = es.lookup({ &jd }, mangle("main"));
cantFail(f.takeError());
return reinterpret_cast<int (*)()>(f->getAddress())();
}
Test .ll file
; ModuleID = 'sum.ll'
source_filename = "sum.c"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc19.29.30133"
%struct._iobuf = type { i8* }
%struct.__crt_locale_pointers = type { %struct.__crt_locale_data*, %struct.__crt_multibyte_data* }
%struct.__crt_locale_data = type opaque
%struct.__crt_multibyte_data = type opaque
$scanf = comdat any
$__local_stdio_scanf_options = comdat any
$"??_C#_02DPKJAMEF#?$CFd?$AA#" = comdat any
#"??_C#_02DPKJAMEF#?$CFd?$AA#" = linkonce_odr dso_local unnamed_addr constant [3 x i8] c"%d\00", comdat, align 1
#__local_stdio_scanf_options._OptionsStorage = internal global i64 0, align 8
; Function Attrs: nounwind uwtable
define dso_local i32 #main() local_unnamed_addr #0 {
%1 = alloca i32, align 4
%2 = bitcast i32* %1 to i8*
call void #llvm.lifetime.start.p0i8(i64 4, i8* nonnull %2) #6
%3 = call i32 (i8*, ...) #scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* #"??_C#_02DPKJAMEF#?$CFd?$AA#", i64 0, i64 0), i32* nonnull %1)
%4 = load i32, i32* %1, align 4, !tbaa !4
%5 = call i32 (i8*, ...) #scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* #"??_C#_02DPKJAMEF#?$CFd?$AA#", i64 0, i64 0), i32* nonnull %1)
%6 = load i32, i32* %1, align 4, !tbaa !4
%7 = add nsw i32 %6, %4
%8 = call i32 (i8*, ...) #scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* #"??_C#_02DPKJAMEF#?$CFd?$AA#", i64 0, i64 0), i32* nonnull %1)
%9 = load i32, i32* %1, align 4, !tbaa !4
%10 = add nsw i32 %7, %9
%11 = call i32 (i8*, ...) #scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* #"??_C#_02DPKJAMEF#?$CFd?$AA#", i64 0, i64 0), i32* nonnull %1)
%12 = load i32, i32* %1, align 4, !tbaa !4
%13 = add nsw i32 %10, %12
%14 = call i32 (i8*, ...) #scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* #"??_C#_02DPKJAMEF#?$CFd?$AA#", i64 0, i64 0), i32* nonnull %1)
%15 = load i32, i32* %1, align 4, !tbaa !4
%16 = add nsw i32 %13, %15
%17 = call i32 (i8*, ...) #scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* #"??_C#_02DPKJAMEF#?$CFd?$AA#", i64 0, i64 0), i32* nonnull %1)
%18 = load i32, i32* %1, align 4, !tbaa !4
%19 = add nsw i32 %16, %18
%20 = call i32 (i8*, ...) #scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* #"??_C#_02DPKJAMEF#?$CFd?$AA#", i64 0, i64 0), i32* nonnull %1)
%21 = load i32, i32* %1, align 4, !tbaa !4
%22 = add nsw i32 %19, %21
%23 = call i32 (i8*, ...) #scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* #"??_C#_02DPKJAMEF#?$CFd?$AA#", i64 0, i64 0), i32* nonnull %1)
%24 = load i32, i32* %1, align 4, !tbaa !4
%25 = add nsw i32 %22, %24
%26 = call i32 (i8*, ...) #scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* #"??_C#_02DPKJAMEF#?$CFd?$AA#", i64 0, i64 0), i32* nonnull %1)
%27 = load i32, i32* %1, align 4, !tbaa !4
%28 = add nsw i32 %25, %27
%29 = call i32 (i8*, ...) #scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* #"??_C#_02DPKJAMEF#?$CFd?$AA#", i64 0, i64 0), i32* nonnull %1)
%30 = load i32, i32* %1, align 4, !tbaa !4
%31 = add nsw i32 %28, %30
call void #llvm.lifetime.end.p0i8(i64 4, i8* nonnull %2) #6
ret i32 %31
}
; Function Attrs: argmemonly mustprogress nofree nosync nounwind willreturn
declare void #llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #1
; Function Attrs: inlinehint nobuiltin nounwind uwtable
define linkonce_odr dso_local i32 #scanf(i8* %0, ...) local_unnamed_addr #2 comdat {
%2 = alloca i8*, align 8
%3 = bitcast i8** %2 to i8*
call void #llvm.lifetime.start.p0i8(i64 8, i8* nonnull %3) #6
call void #llvm.va_start(i8* nonnull %3)
%4 = load i8*, i8** %2, align 8, !tbaa !8
%5 = call %struct._iobuf* #__acrt_iob_func(i32 0) #6
%6 = call i64* #__local_stdio_scanf_options() #6
%7 = load i64, i64* %6, align 8, !tbaa !10
%8 = call i32 #__stdio_common_vfscanf(i64 %7, %struct._iobuf* %5, i8* %0, %struct.__crt_locale_pointers* null, i8* %4) #6
call void #llvm.va_end(i8* nonnull %3)
call void #llvm.lifetime.end.p0i8(i64 8, i8* nonnull %3) #6
ret i32 %8
}
; Function Attrs: argmemonly mustprogress nofree nosync nounwind willreturn
declare void #llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #1
; Function Attrs: mustprogress nofree nosync nounwind willreturn
declare void #llvm.va_start(i8*) #3
; Function Attrs: mustprogress nofree nosync nounwind willreturn
declare void #llvm.va_end(i8*) #3
declare dso_local %struct._iobuf* #__acrt_iob_func(i32) local_unnamed_addr #4
declare dso_local i32 #__stdio_common_vfscanf(i64, %struct._iobuf*, i8*, %struct.__crt_locale_pointers*, i8*) local_unnamed_addr #4
; Function Attrs: noinline nounwind uwtable
define linkonce_odr dso_local i64* #__local_stdio_scanf_options() local_unnamed_addr #5 comdat {
ret i64* #__local_stdio_scanf_options._OptionsStorage
}
attributes #0 = { nounwind uwtable "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { argmemonly mustprogress nofree nosync nounwind willreturn }
attributes #2 = { inlinehint nobuiltin nounwind uwtable "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { mustprogress nofree nosync nounwind willreturn }
attributes #4 = { "frame-pointer"="none" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { noinline nounwind uwtable "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #6 = { nounwind }
!llvm.module.flags = !{!0, !1, !2}
!llvm.ident = !{!3}
!0 = !{i32 1, !"wchar_size", i32 2}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{i32 7, !"uwtable", i32 1}
!3 = !{!"clang version 13.0.1"}
!4 = !{!5, !5, i64 0}
!5 = !{!"int", !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long long", !6, i64 0}
Which is compiled from:
#include <stdio.h>
int
main()
{
int n, sum = 0;
for (int i = 0; i < 10; ++i) {
scanf("%d", &n);
sum += n;
}
return sum;
}
Well, it turns out that THERE IS NOTHING WRONG WITH MY CODE!
I compile and test the same code in Linux environment (WSL2), and everything works fine. I'm pretty sure that this is somewhat compatibility problem between Linux and Windows.
Maybe this is a bug of LLVM?

Getting the value stored from register in llvm IR

I have a simple C program.
int
1.main(int argc, char **argv) {
2. unsigned buffer[4] = { 0, 0, 0, 0 };
3. return buffer[argc];
4. }
And the IR code is as below
; Function Attrs: norecurse nounwind readnone uwtable
define i32 #main(i32 %argc, i8** nocapture readnone %argv) #0 !dbg !6 {
%buffer = alloca [4 x i32], align 16
tail call void #llvm.dbg.value(metadata i32 %argc, i64 0, metadata !14, metadata !21), !dbg !22
tail call void #llvm.dbg.value(metadata i8** %argv, i64 0, metadata !15, metadata !21), !dbg !23
%1 = bitcast [4 x i32]* %buffer to i8*, !dbg !24
call void #llvm.lifetime.start(i64 16, i8* %1) #3, !dbg !24
tail call void #llvm.dbg.declare(metadata [4 x i32]* %buffer, metadata !16, metadata !21), !dbg !25
call void #llvm.memset.p0i8.i64(i8* %1, i8 0, i64 16, i32 16, i1 false), !dbg !26
%2 = sext i32 %argc to i64, !dbg !28
%3 = getelementptr inbounds [4 x i32], [4 x i32]* %buffer, i64 0, i64 %2, !dbg !28
%4 = load i32, i32* %3, align 4, !dbg !28, !tbaa !29
call void #llvm.lifetime.end(i64 16, i8* %1) #3, !dbg !33
ret i32 %4, !dbg !34
}
I want to compare whether the accessing index at line 3 is a valid index. For this comparison I need to extract the value stored for the argc. Below is piece of code I have written to obain the vvalue of argc
auto gep = llvm::dyn_cast<llvm::GetElementPtrInst>(inst);
auto operand2 = gep->getOperand(2);
outs() << "operand 2 "<<*operand2<<"\n";
auto newOperand =operand2.getOperand(0);
outs()<<"New operand "<<*newOperand<<"\n";
Output :-
operand 2 %2 = sext i32 %argc to i64, !dbg !28
New operand i32 %argc
How can I get the value of %argc?
The return value of getOperand function is Value* object of the argc variable (variable newOperand in your code). You can pass that value to any new instruction that you might want to inject (for example CreateICmpEQ) in the IR to compare value of argc with some constant value.

llvm exceptions - RaiseException "?:Unknown signal"

I'm trying to get exceptions working with llvm for a very simple example, that I can later build on but i'm running into some real difficulties and i'm not sure why.
I got clang to give me the following llir code, that I am passing into the MCJIT
; llvm-as c++exn.ll && llvm-ld -native c++exn.bc -lstdc++.6 && ./a.out
%"class.std::ios_base::Init" = type { i8 }
%"class.std::basic_ostream" = type { i32 (...)**, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", %"class.std::basic_ostream"*, i8, i8, %"class.std::basic_streambuf"*, %"class.std::ctype"*, %"class.std::num_put"*, %"class.std::num_get"* }
%"class.std::ios_base" = type { i32 (...)**, i64, i64, i32, i32, i32, %"struct.std::ios_base::_Callback_list"*, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, %"struct.std::ios_base::_Words"*, %"class.std::locale" }
%"struct.std::ios_base::_Callback_list" = type { %"struct.std::ios_base::_Callback_list"*, void (i32, %"class.std::ios_base"*, i32)*, i32, i32 }
%"struct.std::ios_base::_Words" = type <{ i8*, i32, [4 x i8] }>
%"class.std::locale" = type { %"class.std::locale::_Impl"* }
%"class.std::locale::_Impl" = type { i32, %"class.std::locale::facet"**, i64, %"class.std::locale::facet"**, i8** }
%"class.std::locale::facet" = type <{ i32 (...)**, i32, [4 x i8] }>
%"class.std::basic_streambuf" = type { i32 (...)**, i8*, i8*, i8*, i8*, i8*, i8*, %"class.std::locale" }
%"class.std::ctype" = type <{ %"class.std::locale::facet.base", [4 x i8], i32*, i8, [7 x i8], i32*, i32*, i16*, i8, [256 x i8], [256 x i8], i8, [6 x i8] }>
%"class.std::locale::facet.base" = type <{ i32 (...)**, i32 }>
%"class.std::num_put" = type { %"class.std::locale::facet.base", [4 x i8] }
%"class.std::num_get" = type { %"class.std::locale::facet.base", [4 x i8] }
%class.new_exception = type { %"class.std::exception" }
%"class.std::exception" = type { i32 (...)** }
$_ZN13new_exceptionC2Ev = comdat any
$_ZN13new_exceptionD2Ev = comdat any
$__clang_call_terminate = comdat any
$_ZNSt9exceptionC2Ev = comdat any
$_ZN13new_exceptionD0Ev = comdat any
$_ZTS13new_exception = comdat any
$_ZTI13new_exception = comdat any
$_ZTV13new_exception = comdat any
#_ZStL8__ioinit = internal global %"class.std::ios_base::Init" zeroinitializer, align 1
#_ZTVN10__cxxabiv120__si_class_type_infoE = external global i8*
#_ZTS13new_exception = linkonce_odr constant [16 x i8] c"13new_exception\00", comdat
#_ZTISt9exception = external constant i8*
#_ZTI13new_exception = linkonce_odr constant { i8*, i8*, i8* } { i8* bitcast (i8** getelementptr inbounds (i8*, i8** #_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([16 x i8], [16 x i8]* #_ZTS13new_exception, i32 0, i32 0), i8* bitcast (i8** #_ZTISt9exception to i8*) }, comdat
#_ZSt4cout = external global %"class.std::basic_ostream", align 8
#.str = private unnamed_addr constant [11 x i8] c"caught !!!\00", align 1
#_ZTV13new_exception = linkonce_odr unnamed_addr constant [5 x i8*] [i8* null, i8* bitcast ({ i8*, i8*, i8* }* #_ZTI13new_exception to i8*), i8* bitcast (void (%class.new_exception*)* #_ZN13new_exceptionD2Ev to i8*), i8* bitcast (void (%class.new_exception*)* #_ZN13new_exceptionD0Ev to i8*), i8* bitcast (i8* (%"class.std::exception"*)* #_ZNKSt9exception4whatEv to i8*)], comdat, align 8
#_ZTVSt9exception = external unnamed_addr constant [5 x i8*]
#llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* #_GLOBAL__sub_I_runtime_gen.cpp, i8* null }]
; Function Attrs: uwtable
define internal void #__cxx_global_var_init() #0 {
call void #_ZNSt8ios_base4InitC1Ev(%"class.std::ios_base::Init"* #_ZStL8__ioinit)
%1 = call i32 #atexit(void ()* #__dtor__ZStL8__ioinit) #2
ret void
}
declare void #_ZNSt8ios_base4InitC1Ev(%"class.std::ios_base::Init"*) #1
declare void #_ZNSt8ios_base4InitD1Ev(%"class.std::ios_base::Init"*) #1
; Function Attrs: uwtable
define internal void #__dtor__ZStL8__ioinit() #0 {
call void #_ZNSt8ios_base4InitD1Ev(%"class.std::ios_base::Init"* #_ZStL8__ioinit)
ret void
}
; Function Attrs: nounwind
declare i32 #atexit(void ()*) #2
; Function Attrs: uwtable
define void #_Z5functv() #0 {
%1 = call i8* #__cxa_allocate_exception(i64 8) #2
%2 = bitcast i8* %1 to %class.new_exception*
%3 = bitcast %class.new_exception* %2 to i8*
call void #llvm.memset.p0i8.i64(i8* %3, i8 0, i64 8, i32 16, i1 false)
call void #_ZN13new_exceptionC2Ev(%class.new_exception* %2) #2
call void #__cxa_throw(i8* %1, i8* bitcast ({ i8*, i8*, i8* }* #_ZTI13new_exception to i8*), i8* bitcast (void (%class.new_exception*)* #_ZN13new_exceptionD2Ev to i8*)) #11
unreachable
; No predecessors!
ret void
}
declare i8* #__cxa_allocate_exception(i64)
; Function Attrs: argmemonly nounwind
declare void #llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) #3
; Function Attrs: inlinehint nounwind uwtable
define linkonce_odr void #_ZN13new_exceptionC2Ev(%class.new_exception* %this) unnamed_addr #4 comdat align 2 {
%1 = alloca %class.new_exception*, align 8
store %class.new_exception* %this, %class.new_exception** %1, align 8
%2 = load %class.new_exception*, %class.new_exception** %1, align 8
%3 = bitcast %class.new_exception* %2 to %"class.std::exception"*
call void #_ZNSt9exceptionC2Ev(%"class.std::exception"* %3) #2
%4 = bitcast %class.new_exception* %2 to i32 (...)***
store i32 (...)** bitcast (i8** getelementptr inbounds ([5 x i8*], [5 x i8*]* #_ZTV13new_exception, i64 0, i64 2) to i32 (...)**), i32 (...)*** %4, align 8
ret void
}
; Function Attrs: inlinehint nounwind uwtable
define linkonce_odr void #_ZN13new_exceptionD2Ev(%class.new_exception* %this) unnamed_addr #4 comdat align 2 {
%1 = alloca %class.new_exception*, align 8
store %class.new_exception* %this, %class.new_exception** %1, align 8
%2 = load %class.new_exception*, %class.new_exception** %1, align 8
%3 = bitcast %class.new_exception* %2 to %"class.std::exception"*
call void #_ZNSt9exceptionD2Ev(%"class.std::exception"* %3) #2
ret void
}
declare void #__cxa_throw(i8*, i8*, i8*)
; Function Attrs: uwtable
define void #_Z4ctchv() #0 personality i8* bitcast (i32 (...)* #__gxx_personality_seh0 to i8*) {
%1 = alloca i8*
%2 = alloca i32
%o = alloca %class.new_exception*, align 8
invoke void #_Z5functv()
to label %3 unwind label %4
; <label>:3 ; preds = %0
br label %20
; <label>:4 ; preds = %0
%5 = landingpad { i8*, i32 }
catch i8* bitcast ({ i8*, i8*, i8* }* #_ZTI13new_exception to i8*)
%6 = extractvalue { i8*, i32 } %5, 0
store i8* %6, i8** %1, align 8
%7 = extractvalue { i8*, i32 } %5, 1
store i32 %7, i32* %2, align 4
br label %8
; <label>:8 ; preds = %4
%9 = load i32, i32* %2, align 4
%10 = call i32 #llvm.eh.typeid.for(i8* bitcast ({ i8*, i8*, i8* }* #_ZTI13new_exception to i8*)) #2
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %26
; <label>:12 ; preds = %8
%13 = load i8*, i8** %1, align 8
%14 = call i8* #__cxa_begin_catch(i8* %13) #2
%15 = bitcast i8* %14 to %class.new_exception*
store %class.new_exception* %15, %class.new_exception** %o, align 8
%16 = invoke dereferenceable(272) %"class.std::basic_ostream"* #_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"* dereferenceable(272) #_ZSt4cout, i8* getelementptr inbounds ([11 x i8], [11 x i8]* #.str, i32 0, i32 0))
to label %17 unwind label %21
; <label>:17 ; preds = %12
%18 = invoke dereferenceable(272) %"class.std::basic_ostream"* #_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"* %16, %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)* #_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
to label %19 unwind label %21
; <label>:19 ; preds = %17
call void #__cxa_end_catch()
br label %20
; <label>:20 ; preds = %19, %3
ret void
; <label>:21 ; preds = %17, %12
%22 = landingpad { i8*, i32 }
cleanup
%23 = extractvalue { i8*, i32 } %22, 0
store i8* %23, i8** %1, align 8
%24 = extractvalue { i8*, i32 } %22, 1
store i32 %24, i32* %2, align 4
invoke void #__cxa_end_catch()
to label %25 unwind label %31
; <label>:25 ; preds = %21
br label %26
; <label>:26 ; preds = %25, %8
%27 = load i8*, i8** %1, align 8
%28 = load i32, i32* %2, align 4
%29 = insertvalue { i8*, i32 } undef, i8* %27, 0
%30 = insertvalue { i8*, i32 } %29, i32 %28, 1
resume { i8*, i32 } %30
; <label>:31 ; preds = %21
%32 = landingpad { i8*, i32 }
catch i8* null
%33 = extractvalue { i8*, i32 } %32, 0
call void #__clang_call_terminate(i8* %33) #12
unreachable
}
declare i32 #__gxx_personality_seh0(...)
; Function Attrs: nounwind readnone
declare i32 #llvm.eh.typeid.for(i8*) #5
declare i8* #__cxa_begin_catch(i8*)
declare dereferenceable(272) %"class.std::basic_ostream"* #_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(%"class.std::basic_ostream"* dereferenceable(272), i8*) #1
declare dereferenceable(272) %"class.std::basic_ostream"* #_ZNSolsEPFRSoS_E(%"class.std::basic_ostream"*, %"class.std::basic_ostream"* (%"class.std::basic_ostream"*)*) #1
declare dereferenceable(272) %"class.std::basic_ostream"* #_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(%"class.std::basic_ostream"* dereferenceable(272)) #1
declare void #__cxa_end_catch()
; Function Attrs: noinline noreturn nounwind
define linkonce_odr hidden void #__clang_call_terminate(i8*) #6 comdat {
%2 = call i8* #__cxa_begin_catch(i8* %0) #2
call void #_ZSt9terminatev() #12
unreachable
}
declare void #_ZSt9terminatev()
; Function Attrs: norecurse uwtable
define i32 #main() #7 {
call void #_Z4ctchv()
ret i32 0
}
; Function Attrs: nounwind uwtable
define linkonce_odr void #_ZNSt9exceptionC2Ev(%"class.std::exception"* %this) unnamed_addr #8 comdat align 2 {
%1 = alloca %"class.std::exception"*, align 8
store %"class.std::exception"* %this, %"class.std::exception"** %1, align 8
%2 = load %"class.std::exception"*, %"class.std::exception"** %1, align 8
%3 = bitcast %"class.std::exception"* %2 to i32 (...)***
store i32 (...)** bitcast (i8** getelementptr inbounds ([5 x i8*], [5 x i8*]* #_ZTVSt9exception, i64 0, i64 2) to i32 (...)**), i32 (...)*** %3, align 8
ret void
}
; Function Attrs: inlinehint nounwind uwtable
define linkonce_odr void #_ZN13new_exceptionD0Ev(%class.new_exception* %this) unnamed_addr #4 comdat align 2 {
%1 = alloca %class.new_exception*, align 8
store %class.new_exception* %this, %class.new_exception** %1, align 8
%2 = load %class.new_exception*, %class.new_exception** %1, align 8
call void #_ZN13new_exceptionD2Ev(%class.new_exception* %2) #2
%3 = bitcast %class.new_exception* %2 to i8*
call void #_ZdlPv(i8* %3) #13
ret void
}
; Function Attrs: nounwind
declare i8* #_ZNKSt9exception4whatEv(%"class.std::exception"*) #9
; Function Attrs: nobuiltin nounwind
declare void #_ZdlPv(i8*) #10
; Function Attrs: nounwind
declare void #_ZNSt9exceptionD2Ev(%"class.std::exception"*) #9
; Function Attrs: uwtable
define internal void #_GLOBAL__sub_I_runtime_gen.cpp() #0 {
call void #__cxx_global_var_init()
ret void
}
attributes #0 = { uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { nounwind }
attributes #3 = { argmemonly nounwind }
attributes #4 = { inlinehint nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #5 = { nounwind readnone }
attributes #6 = { noinline noreturn nounwind }
attributes #7 = { norecurse uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #8 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #9 = { nounwind "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #10 = { nobuiltin nounwind "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #11 = { noreturn }
attributes #12 = { noreturn nounwind }
attributes #13 = { builtin nounwind }
This is the llir code generated from the following C++ code.
#include <iostream>
#include <exception>
using namespace std;
class new_exception : public exception {
const char* what() {
return "new_exception";
}
};
void funct() {
throw new_exception();
}
void ctch() {
try {
funct();
} catch(new_exception& o) {
std::cout << "caught !!!" << std::endl;
}
}
int main() {
ctch();
}
When loaded it compiled by MCJIT on windows without issue but when run it crashes, when run with gdb it halts with message 'RaiseException() ?:Unknown signal'.
As far as I am aware the exception is being handled correctly, "caught" by the code and so shouldn't be the issue.
Is my use of the personality function correct, I have looked through answers that suggest the use of SEH based personality on windows might be wrong. What is the alternative if this is wrong and why would clang write incorrect code?
Platform is Windows X86_64
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"
I'm starting to use exceptions in C++ for Windows with clang and msvc 2015, and they do work very well with standard C++ exceptions (that Microsoft documentation calls synchronous) -- no need to use SEH (AKA Structured Exception Handling), that Microsoft calls asynchronous.
Your Code contain an error. The main method doesn't return an int, please check it:
Change this
int main() {
ctch();
}
By this:
int main() {
ctch();
return 0;
}

LLVM Create VarArg Function and access var args

I have been trying to create a function using the module pass in LLVM. What I am trying to do is create a variable argument function and then add the logic to manipulate the variable arguments.
For example:
/\*can do this\*/
int foo(int a, ...)
{
double var1;
//can't figure out how to add any of this using llvm
va_list ap;
va_start(ap, a);
va_arg(var1,double);
va_end(ap);
}
Creating the function type is easy because I just set the vararg boolean to true. What do I do after that?
I always use clang to check what it needs to convert for c/c++ lang.
Use llvm instruction va_arg and intinsics llvm.va_start, llvm.va_end, llvm.va_copy to use llvm variable argument support.
you also need target-specific value type “va_list” for functions that operates on arguments that use this.
; This struct is different for every platform. For most platforms,
; it is merely an i8*.
%struct.va_list = type { i8* }
; For Unix x86_64 platforms, va_list is the following struct:
; %struct.va_list = type { i32, i32, i8*, i8* }
ref http://llvm.org/docs/LangRef.html#variable-argument-handling-intrinsics
for your listed code,
; ModuleID = 'test.c'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
%struct.__va_list_tag = type { i32, i32, i8*, i8* }
; Function Attrs: nounwind uwtable
define i32 #foo(i32 %a, ...) #0 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
%var1 = alloca double, align 8
%ap = alloca [1 x %struct.__va_list_tag], align 16
store i32 %a, i32* %2, align 4
%3 = getelementptr inbounds [1 x %struct.__va_list_tag]* %ap, i32 0, i32 0
%4 = bitcast %struct.__va_list_tag* %3 to i8*
call void #llvm.va_start(i8* %4)
%5 = getelementptr inbounds [1 x %struct.__va_list_tag]* %ap, i32 0, i32 0
%6 = getelementptr inbounds %struct.__va_list_tag* %5, i32 0, i32 1
%7 = load i32* %6
%8 = icmp ule i32 %7, 160
br i1 %8, label %9, label %15
; <label>:9 ; preds = %0
%10 = getelementptr inbounds %struct.__va_list_tag* %5, i32 0, i32 3
%11 = load i8** %10
%12 = getelementptr i8* %11, i32 %7
%13 = bitcast i8* %12 to double*
%14 = add i32 %7, 16
store i32 %14, i32* %6
br label %20
; <label>:15 ; preds = %0
%16 = getelementptr inbounds %struct.__va_list_tag* %5, i32 0, i32 2
%17 = load i8** %16
%18 = bitcast i8* %17 to double*
%19 = getelementptr i8* %17, i32 8
store i8* %19, i8** %16
br label %20
; <label>:20 ; preds = %15, %9
%21 = phi double* [ %13, %9 ], [ %18, %15 ]
%22 = load double* %21
%23 = getelementptr inbounds [1 x %struct.__va_list_tag]* %ap, i32 0, i32 0
%24 = bitcast %struct.__va_list_tag* %23 to i8*
call void #llvm.va_end(i8* %24)
%25 = load i32* %1
ret i32 %25
}
; Function Attrs: nounwind
declare void #llvm.va_start(i8*) #1
; Function Attrs: nounwind
declare void #llvm.va_end(i8*) #1
; Function Attrs: nounwind uwtable
define i32 #main() #0 {
ret i32 0
}

lli won't take kindly to rust's LLVM IR

I have the following rust code.
$ cat hello.rs
fn main() {
println!("Hello world!");
}
$ rustc hello.rs; ./hello
Hello world!
And I'm producing llvm byte code with the --emit=ir option.
$ rustc --emit=ir hello.rs
$ cat hello.ll
; ModuleID = 'hello.rs'
target datalayout = "e-i64:64-f80:128-n8:16:32:64"
target triple = "x86_64-apple-darwin"
%str_slice = type { i8*, i64 }
%"struct.core::fmt::Argument<[]>[#3]" = type { %"enum.core::result::Result<[(), core::fmt::FormatError]>[#3]" (%"enum.core::fmt::Void<[]>[#3]"*, %"struct.core::fmt::Formatter<[]>[#3]"*)*, %"enum.core::fmt::Void<[]>[#3]"* }
%"enum.core::result::Result<[(), core::fmt::FormatError]>[#3]" = type { i8, [0 x i8], [1 x i8] }
%"struct.core::fmt::Formatter<[]>[#3]" = type { i64, i32, i8, %"enum.core::option::Option<[uint]>[#3]", %"enum.core::option::Option<[uint]>[#3]", { void (i8*)**, i8* }, %"struct.core::slice::Items<[core::fmt::Argument]>[#3]", { %"struct.core::fmt::Argument<[]>[#3]"*, i64 } }
%"enum.core::option::Option<[uint]>[#3]" = type { i8, [7 x i8], [1 x i64] }
%"struct.core::slice::Items<[core::fmt::Argument]>[#3]" = type { %"struct.core::fmt::Argument<[]>[#3]"*, %"struct.core::fmt::Argument<[]>[#3]"*, %"struct.core::kinds::marker::ContravariantLifetime<[]>[#3]" }
%"struct.core::kinds::marker::ContravariantLifetime<[]>[#3]" = type {}
%"enum.core::fmt::Void<[]>[#3]" = type {}
%"struct.core::fmt::Arguments<[]>[#3]" = type { { %"enum.core::fmt::rt::Piece<[]>[#3]"*, i64 }, { %"struct.core::fmt::Argument<[]>[#3]"*, i64 } }
%"enum.core::fmt::rt::Piece<[]>[#3]" = type { i8, [7 x i8], [8 x i64] }
#str1364 = internal constant [12 x i8] c"Hello world!"
#_ZN4main15__STATIC_FMTSTR20h3b67a4ad8efbb398oaaE = internal unnamed_addr constant { { i8, %str_slice, [48 x i8] } } { { i8, %str_slice, [48 x i8] } { i8 0, %str_slice { i8* getelementptr inbounds ([12 x i8]* #str1364, i32 0, i32 0), i64 12 }, [48 x i8] undef } }
; Function Attrs: uwtable
define internal void #_ZN4main20he3565cca0bc2f101eaaE() unnamed_addr #0 {
entry-block:
%match = alloca {}
%__args_vec = alloca { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }
%0 = alloca %"struct.core::fmt::Argument<[]>[#3]", i64 0
%__args = alloca %"struct.core::fmt::Arguments<[]>[#3]"
%__adjust = alloca { %"enum.core::fmt::rt::Piece<[]>[#3]"*, i64 }
%__adjust1 = alloca { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }
br label %case_body
case_body: ; preds = %entry-block
%1 = getelementptr inbounds { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %__args_vec, i32 0, i32 0
store %"struct.core::fmt::Argument<[]>[#3]"* %0, %"struct.core::fmt::Argument<[]>[#3]"** %1
%2 = getelementptr inbounds { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %__args_vec, i32 0, i32 1
store i64 0, i64* %2
%3 = getelementptr inbounds { %"enum.core::fmt::rt::Piece<[]>[#3]"*, i64 }* %__adjust, i32 0, i32 0
store %"enum.core::fmt::rt::Piece<[]>[#3]"* getelementptr inbounds ([1 x %"enum.core::fmt::rt::Piece<[]>[#3]"]* bitcast ({ { i8, %str_slice, [48 x i8] } }* #_ZN4main15__STATIC_FMTSTR20h3b67a4ad8efbb398oaaE to [1 x %"enum.core::fmt::rt::Piece<[]>[#3]"]*), i32 0, i32 0), %"enum.core::fmt::rt::Piece<[]>[#3]"** %3
%4 = getelementptr inbounds { %"enum.core::fmt::rt::Piece<[]>[#3]"*, i64 }* %__adjust, i32 0, i32 1
store i64 1, i64* %4
%5 = getelementptr inbounds { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %__args_vec, i32 0, i32 0
%6 = load %"struct.core::fmt::Argument<[]>[#3]"** %5
%7 = getelementptr inbounds { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %__args_vec, i32 0, i32 1
%8 = load i64* %7
%9 = getelementptr inbounds { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %__adjust1, i32 0, i32 0
store %"struct.core::fmt::Argument<[]>[#3]"* %6, %"struct.core::fmt::Argument<[]>[#3]"** %9
%10 = getelementptr inbounds { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %__adjust1, i32 0, i32 1
store i64 %8, i64* %10
call void #"_ZN3fmt22Arguments$LT$$x27a$GT$3new20h30af698883d0f4c86aaE"(%"struct.core::fmt::Arguments<[]>[#3]"* noalias nocapture sret dereferenceable(32) %__args, { %"enum.core::fmt::rt::Piece<[]>[#3]"*, i64 }* noalias nocapture dereferenceable(16) %__adjust, { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* noalias nocapture dereferenceable(16) %__adjust1)
call void #_ZN2io5stdio12println_args20hecac3fc58fb73442EvmE(%"struct.core::fmt::Arguments<[]>[#3]"* noalias nocapture dereferenceable(32) %__args)
br label %join
join: ; preds = %case_body
ret void
}
define i64 #main(i64, i8**) unnamed_addr #1 {
top:
%2 = call i64 #_ZN10lang_start20h7823875e69d425d0BueE(i8* bitcast (void ()* #_ZN4main20he3565cca0bc2f101eaaE to i8*), i64 %0, i8** %1)
ret i64 %2
}
declare i64 #_ZN10lang_start20h7823875e69d425d0BueE(i8*, i64, i8**) unnamed_addr #1
; Function Attrs: inlinehint uwtable
define internal void #"_ZN3fmt22Arguments$LT$$x27a$GT$3new20h30af698883d0f4c86aaE"(%"struct.core::fmt::Arguments<[]>[#3]"* noalias nocapture sret dereferenceable(32), { %"enum.core::fmt::rt::Piece<[]>[#3]"*, i64 }* noalias nocapture dereferenceable(16), { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* noalias nocapture dereferenceable(16)) unnamed_addr #2 {
entry-block:
%__adjust = alloca { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }
%3 = getelementptr inbounds %"struct.core::fmt::Arguments<[]>[#3]"* %0, i32 0, i32 0
%4 = bitcast { %"enum.core::fmt::rt::Piece<[]>[#3]"*, i64 }* %1 to i8*
%5 = bitcast { %"enum.core::fmt::rt::Piece<[]>[#3]"*, i64 }* %3 to i8*
call void #llvm.memcpy.p0i8.p0i8.i64(i8* %5, i8* %4, i64 16, i32 8, i1 false)
%6 = getelementptr inbounds %"struct.core::fmt::Arguments<[]>[#3]"* %0, i32 0, i32 1
%7 = getelementptr inbounds { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %2, i32 0, i32 0
%8 = load %"struct.core::fmt::Argument<[]>[#3]"** %7
%9 = getelementptr inbounds { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %2, i32 0, i32 1
%10 = load i64* %9
%11 = getelementptr inbounds { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %__adjust, i32 0, i32 0
store %"struct.core::fmt::Argument<[]>[#3]"* %8, %"struct.core::fmt::Argument<[]>[#3]"** %11
%12 = getelementptr inbounds { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %__adjust, i32 0, i32 1
store i64 %10, i64* %12
%13 = bitcast { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %__adjust to i8*
%14 = bitcast { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* %6 to i8*
call void #llvm.memcpy.p0i8.p0i8.i64(i8* %14, i8* %13, i64 16, i32 8, i1 false)
ret void
}
; Function Attrs: nounwind
declare void #llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32, i1) unnamed_addr #3
declare void #_ZN2io5stdio12println_args20hecac3fc58fb73442EvmE(%"struct.core::fmt::Arguments<[]>[#3]"* noalias nocapture dereferenceable(32)) unnamed_addr #1
attributes #0 = { uwtable "split-stack" }
attributes #1 = { "split-stack" }
attributes #2 = { inlinehint uwtable "split-stack" }
attributes #3 = { nounwind "split-stack" }
However, lli won't accept this bytecode.
$ lli hello.ll
lli: hello.ll:47:138: error: expected value token
call void #"_ZN3fmt22Arguments$LT$$x27a$GT$3new20h30af698883d0f4c86aaE"(%"struct.core::fmt::Arguments<[]>[#3]"* noalias nocapture sret dereferenceable(32) %__args, { %"enum.core::fmt::rt::Piece<[]>[#3]"*, i64 }* noalias nocapture dereferenceable(16) %__adjust, { %"struct.core::fmt::Argument<[]>[#3]"*, i64 }* noalias nocapture dereferenceable(16) %__adjust1)
^
Any ideas why?
The dereferenceable attribute was added to LLVM just last month (July 2014). I'm assuming the rustc you are using is based on brand new LLVM code while your lli is slightly older. To fix this, update your code and rebuild.
The dereferenceable attribute was added in a commit one month before the OP, so if you are using a released LLVM package, you may not be using a recent enough package.
Try using an LLVM package built from top of trunk.