sepavloff added a comment.

Consider a bit more complicated example.

File `common.h`

  #ifndef COMMON_H
  #define COMMON_H
  struct Alarm { };
  struct Info { };
  template<typename T> struct Handler;
  #endif

File `use-1.cpp`

  #include "common.h"
  Handler<Info> *info;
  void set(Handler<Info> *e) { info = e; }

File `use-2.cpp`

  #include "common.h"
  Handler<Alarm> *alarm;
  void set(Handler<Alarm> *e) { alarm = e; }

File `defs.h`

  #ifndef DEFS_H
  #define DEFS_H
  template<> struct Handler<Info> { int val; };
  template<> struct Handler<Alarm> { char val; };
  void set(Handler<Info> *);
  void set(Handler<Alarm> *);
  #endif

File `main.cpp`

  #include "common.h"
  #include "defs.h"
  void init() {
    Handler<Info> i;
    set(&i);
    Handler<Alarm> a;
    set(&a);
  }

Process these files with commands:

  clang -c -emit-llvm use-1.cpp -o use-1.cpp.bc
  clang -c -emit-llvm use-2.cpp -o use-2.cpp.bc
  clang -c -emit-llvm main.cpp -o main.cpp.bc
  llvm-link use-1.cpp.bc use-2.cpp.bc main.cpp.bc -S -o test.ll

The resulting IR file has deficiencies:

- The types of the global variables `info` and `alarm` are the same:

  %struct.Event = type { i32 }
  %struct.Event.0 = type { i8 }
  @info = global %struct.Event* null, align 8
  @alarm = global %struct.Event* null, align 8

- The signatures of the `set` functions are the same:

  define void @_Z4fireP5EventI4InfoE(%struct.Event* %e) #0 {
  …
  }
  
  define void @_Z4fireP5EventI5AlarmE(%struct.Event* %e) #0 {
  …
  }

- The call to `set(Handler<Alarm> *)` is augmented by a bitcast to 'fix' its 
type:

  define void @_Z4fireP5EventI5AlarmE(%struct.Event* %e) #0 {
  …
  call void bitcast (void (%struct.Event*)* @_Z4fireP5EventI5AlarmE to void 
(%struct.Event.0*)*)(%struct.Event.0* %a)
  …
  }

The IR created by `llvm-link` is distorted. Although code generation (in LTO 
compilation) might be unaffected by this distortions, other applications of IR 
linking suffer from it. It does not allow to implement some checks, validation 
techniques and optimizations.


https://reviews.llvm.org/D40567



_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to