4

In LLVM-3.0, named structs are always unique and pointer equality with other structurally same structs does not work. From their blog entry on LLVM-3.0 types, the highlights are mine:

Identified structures are the kind we are talking about: they can have a name, and can have their body specified after the type is created. The identified structure is not uniqued with other structure types, which is why they are produced with StructType::create(...). Because identified types are potentially recursive, the asmprinter always prints them by their name (or a number like %42 if the identified struct has no name).

This breaks type equality checking by type pointer checking. For example, the haskell package llvm depends on llvm type pointers being equal for compile time type checks and type casts.

Is there any way to check for two structs being isomorphic(same structure)? Preferably in the llvm-c api?

arrowd
  • 33,231
  • 8
  • 79
  • 110
Chetan
  • 448
  • 2
  • 10
  • I'm not sure this question makes sense... two different named struct types are *different*; LLVM doesn't ever try to compare the contents of two structs with different names. Why does your code care? – servn May 04 '12 at 16:38
  • Lets say you are trying to JIT a function that accesses a struct **S**, and calls another function(that takes an instance of **S** as an arg) in another module. The sensible thing to do is to confirm the types match before you generate the code. The common way to check for type equality is to check the type pointers being equal. This does not work for LLVM-3.0 named structs. This is also required in the llvm linker, where it must be able to map the named structs in each module, so the link can work. See [here](http://nondot.org/sabre/LLVMNotes/TypeSystemRewrite.txt) for more information. – Chetan May 05 '12 at 16:10
  • The LLVM IR linker does in fact have code to do a sort of structural comparison... but it isn't a public API because it's the *only* place in LLVM that tries to use such a comparison. Also, it isn't fundamentally required for the correctness of the linker's algorithm anyway; in general, the linker can just use bitcasts. – servn May 05 '12 at 21:12
  • Just using bitcasts to link would be shoddy for llvm, where the type information is available at link time. It _might_ make sense for C/C++ where type casts are relatively loose. Could you point me to the location of the structural comparison? I would like to try to expose this via llmv-c, if possible. – Chetan May 06 '12 at 15:07
  • http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkModules.cpp?revision=155300&view=markup is the code in question. – servn May 06 '12 at 23:08

1 Answers1

2

In the C++ API, the StructType class has

bool StructType::isLayoutIdentical(StructType *Other) const

This function iterates through the elements of the StructTypes to see if they're equal.

jlstrecker
  • 4,953
  • 3
  • 46
  • 60
  • 2
    Worth mentioning that the function is not doing what one may expect it to do: it compares pointers of the struct member types, but doesn't actually check if the layout is identical. Let's say there are two identical structs A(int, int) and B(int, int). According to the isLayoutIdentical they are identical. However, two other structs Wrap_A(A) and Wrap_B(B) are not identical since their members point to different types in memory. – AlexDenisov Jan 16 '20 at 11:46