Emmiting llvm bytecode from clang: 'byval' attribute for passing objects with nontrivial destructor into a function -


i have source c++ code parse using clang, producing llvm bytecode. point want process file myself... encoudered problem. consider following scenario: - create class nontrivial destructor or copy constructor. - define function, object of class passed parameter, value (no reference or pointer).

in produced bytecode, pointer instead. classes without destructor, parameter annotated 'byval', not in case. result, cannot distinguish if parameter passed value, or pointer.

consider following example:

input file - cpass.cpp:

class c {   public:   int x;   ~c() {} };  void set(c val, int x) {val.x=x;};  void set(c *ptr, int x) {ptr->x=x;} 

compilation command line:

clang++ -c cpass.cpp -emit-llvm -o cpass.bc; llvm-dis cpass.bc 

produced output file (cpass.ll):

; moduleid = 'cpass.bc' target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" target triple = "x86_64-unknown-linux-gnu"  %class.c = type { i32 }  define void @_z3set1ci(%class.c* %val, i32 %x) nounwind {   %1 = alloca i32, align 4   store i32 %x, i32* %1, align 4   %2 = load i32* %1, align 4   %3 = getelementptr inbounds %class.c* %val, i32 0, i32 0   store i32 %2, i32* %3, align 4   ret void }  define void @_z3setp1ci(%class.c* %ptr, i32 %x) nounwind {   %1 = alloca %class.c*, align 8   %2 = alloca i32, align 4   store %class.c* %ptr, %class.c** %1, align 8   store i32 %x, i32* %2, align 4   %3 = load i32* %2, align 4   %4 = load %class.c** %1, align 8   %5 = getelementptr inbounds %class.c* %4, i32 0, i32 0   store i32 %3, i32* %5, align 4   ret void } 

as can see, parameters of both set functions same. how can tell first function meant take parameter value, instead of pointer?

one solution somehow parse mangled function name, may not viable. if puts extern "c" before function?

is there way tell clang keep byval annotation, or produce annotation each function parameter passed value?

anton korobeynikov suggests should dig clang's llvm ir emission. unfortunately know nothing clang internals, documentation rather sparse. internals manual of clang not talk ir emission. don't know how start, go problem solved, without going through all of clang source code. pointers? hints? further reading?


in response anton korobeynikov:

i know more-or-less how c++ abi looks respect of parameter passing. found reading here: http://agner.org./optimize/calling_conventions.pdf. platform dependent! approach might not feasable on different architectures or in special circumstances.

in case, example, function going run on different device being called from. 2 devices don't share memory, don't share stack. unless user passing pointer (in case assume knows doing), object should passed within function-parameters message. if has nontrivial copy constructor, should executed caller, object should created in parameter area well.

so, somehow override abi in clang, without intrusion source code. or maybe add additional annotation, ignored in normal compilation pipeline, detect when parsing .bc/.ll file. or somehow differently reconstruct function signature.


unfortunately, "byval" not "annotation", it's parameter attribute means alot optimizers , backends. basically, rules how pass small structs / classes , without non-trivial functions government platform c++ abi, cannot use byval here.

in fact, byval here result of minor optimization @ frontend level. when you're passing stuff value, temporary object should constructed on stack (via default copy ctor). when have class pod-like, clang can deduce copy ctor trivial , optimize pair of ctor / dtor out, passing "contents".

for non-trivial classes (like in case) clang cannot perform such optimization , have call both ctor , dtor. you're seeing pointer temporary object created.

try call set() functions , you'll see what's going there.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -