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 |1 Answer
Reset to default 0Currently it is not possible to keep struct/class definitions without any usage. clang does not generate them in the first place.
-flto
) discover they are unused and optimize them away. – Nate Eldredge Commented Jan 9 at 3:29