Hello LLVM community,
I want to instrument the CPP macro and add a piece of code there. I tried to check how the macro looks in the llvm IR. But after searching a bit, I figured llvm couldn’t associate macro in the IR. If there is a way to attach the macro in IR, please let me know,
suppose, if I write a macro using #define
preprocessor directive like following,
#define FSM_TRANSITION( _newState, attr... ) \
do \
{ \
std::cerr << state2str( currentState ) << " -> " \
<< state2str( _newState ) << std::endl; \
newState = _newState; \
} \
while( false )
I can treat it as a function and in IR the code will look like following,
; Function Attrs: noinline nounwind optnone uwtable
define void @_Z15macro_functionsv() #0 {
entry:
%currentState = alloca i32, align 4
store i32 0, i32* %currentState, align 4
%0 = load i32, i32* %currentState, align 4
%call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0, i32 1)
%1 = load i32, i32* %currentState, align 4
%2 = add nsw i32 %1, 1
store i32 %2, i32* %currentState, align 4
%3 = load i32, i32* %currentState, align 4
%call1 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %3, i32 2)
ret void
}
declare i32 @printf(i8*, ...) #1
@.str = private unnamed_addr constant [21 x i8] c"Transition from %d to %d\0A\00", align 1
But now if I compile it with -S -emit-llvm
flags to generate the .ll
file, it contains only module header information, there is no function or code inside and looks like following,
; ModuleID = 'macro.cpp'
source_filename = "macro.cpp"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
!llvm.module.flags = !{!0, !1, !2, !3, !4}
!llvm.ident = !{!5}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 1}
!4 = !{i32 7, !"frame-pointer", i32 2}
!5 = !{!"Ubuntu clang version 14.0.6"}
Is it possible to link macro to llvm IR?
Is it possible to instrument the macro without getting the macro definition inside IR?
The point is, how can I add a simple print inside the #define
derivative?