How to compile and keep "unused" CC++ struct definitions with clang `-emit-llvm` - Stack Overflow

ContextI'm working on memory layout detections on CC++ structures. To do so, I collect structure

Context

I'm working on memory layout detections on C/C++ structures. To do so, I collect structures' IR codes and analyse them.

Issue

However, if a structure is not used, clang may ignore it and won't generate any IR codes.

struct A {
    void *ptr;
    int m_buf;
};

struct B {
    struct A a;
    int m_val;
};

int main() {
    // struct A a;
    // struct B b;
    return 0;
}

Here is a simple, if I use struct A&B in the main, and compile it with clang -g -S -emit-llvm, it generates IR codes like:

%struct.A = type { i8*, i32 }
%struct.B = type { %struct.A, i32 }

But if not used, then non LLVM IR will be generated.

I notice earlier question here: How to compile and keep "unused" C declarations with clang -emit-llvm. But it seems to deal with function declarations. And I dive a little bit more into clang's source codes, and found that used structs will go trough functions like Sema::RequireCompleteType. But I cannot find where unused structs are ignored. How can I solve this issue ?


Adding structs usage code works of course, but when the code base becomes large, it can be a great overhead to add the usage code. I want to find the structs ignore code and "turn it off".

Context

I'm working on memory layout detections on C/C++ structures. To do so, I collect structures' IR codes and analyse them.

Issue

However, if a structure is not used, clang may ignore it and won't generate any IR codes.

struct A {
    void *ptr;
    int m_buf;
};

struct B {
    struct A a;
    int m_val;
};

int main() {
    // struct A a;
    // struct B b;
    return 0;
}

Here is a simple, if I use struct A&B in the main, and compile it with clang -g -S -emit-llvm, it generates IR codes like:

%struct.A = type { i8*, i32 }
%struct.B = type { %struct.A, i32 }

But if not used, then non LLVM IR will be generated.

I notice earlier question here: How to compile and keep "unused" C declarations with clang -emit-llvm. But it seems to deal with function declarations. And I dive a little bit more into clang's source codes, and found that used structs will go trough functions like Sema::RequireCompleteType. But I cannot find where unused structs are ignored. How can I solve this issue ?


Adding structs usage code works of course, but when the code base becomes large, it can be a great overhead to add the usage code. I want to find the structs ignore code and "turn it off".

Share Improve this question asked Nov 19, 2024 at 5:11 YinwheYinwhe 453 bronze badges 1
  • What kind of overhead are you concerned about? Compile time, code memory usage, data memory usage, executable size, etc? One possibility would be to create a global object of each of the types, and then let link-time optimization (-flto) discover they are unused and optimize them away. – Nate Eldredge Commented Jan 9 at 3:29
Add a comment  | 

1 Answer 1

Reset to default 0

Currently it is not possible to keep struct/class definitions without any usage. clang does not generate them in the first place.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745580958a4634251.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信