Skip to content

Commit a8bda0b

Browse files
authored
[C API] Add accessors for function prefix and prologue data (#82193)
A test is added to echo.ll, and the echo.cpp part of llvm-c-test is updated to clone a function's prefix and prologue.
1 parent 4294841 commit a8bda0b

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

llvm/docs/ReleaseNotes.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ Changes to the C API
131131
* Added ``LLVMConstStringInContext2`` function, which better matches the C++
132132
API by using ``size_t`` for string length. Deprecated ``LLVMConstStringInContext``.
133133

134+
* Added the following functions for accessing a function's prefix data:
135+
136+
* ``LLVMHasPrefixData``
137+
* ``LLVMGetPrefixData``
138+
* ``LLVMSetPrefixData``
139+
140+
* Added the following functions for accessing a function's prologue data:
141+
142+
* ``LLVMHasPrologueData``
143+
* ``LLVMGetPrologueData``
144+
* ``LLVMSetPrologueData``
145+
134146
Changes to the CodeGen infrastructure
135147
-------------------------------------
136148

llvm/include/llvm-c/Core.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,6 +2748,44 @@ const char *LLVMGetGC(LLVMValueRef Fn);
27482748
*/
27492749
void LLVMSetGC(LLVMValueRef Fn, const char *Name);
27502750

2751+
/**
2752+
* Gets the prefix data associated with a function. Only valid on functions, and
2753+
* only if LLVMHasPrefixData returns true.
2754+
* See https://llvm.org/docs/LangRef.html#prefix-data
2755+
*/
2756+
LLVMValueRef LLVMGetPrefixData(LLVMValueRef Fn);
2757+
2758+
/**
2759+
* Check if a given function has prefix data. Only valid on functions.
2760+
* See https://llvm.org/docs/LangRef.html#prefix-data
2761+
*/
2762+
LLVMBool LLVMHasPrefixData(LLVMValueRef Fn);
2763+
2764+
/**
2765+
* Sets the prefix data for the function. Only valid on functions.
2766+
* See https://llvm.org/docs/LangRef.html#prefix-data
2767+
*/
2768+
void LLVMSetPrefixData(LLVMValueRef Fn, LLVMValueRef prefixData);
2769+
2770+
/**
2771+
* Gets the prologue data associated with a function. Only valid on functions,
2772+
* and only if LLVMHasPrologueData returns true.
2773+
* See https://llvm.org/docs/LangRef.html#prologue-data
2774+
*/
2775+
LLVMValueRef LLVMGetPrologueData(LLVMValueRef Fn);
2776+
2777+
/**
2778+
* Check if a given function has prologue data. Only valid on functions.
2779+
* See https://llvm.org/docs/LangRef.html#prologue-data
2780+
*/
2781+
LLVMBool LLVMHasPrologueData(LLVMValueRef Fn);
2782+
2783+
/**
2784+
* Sets the prologue data for the function. Only valid on functions.
2785+
* See https://llvm.org/docs/LangRef.html#prologue-data
2786+
*/
2787+
void LLVMSetPrologueData(LLVMValueRef Fn, LLVMValueRef prologueData);
2788+
27512789
/**
27522790
* Add an attribute to a function.
27532791
*

llvm/lib/IR/Core.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,38 @@ void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
24222422
F->clearGC();
24232423
}
24242424

2425+
LLVMValueRef LLVMGetPrefixData(LLVMValueRef Fn) {
2426+
Function *F = unwrap<Function>(Fn);
2427+
return wrap(F->getPrefixData());
2428+
}
2429+
2430+
LLVMBool LLVMHasPrefixData(LLVMValueRef Fn) {
2431+
Function *F = unwrap<Function>(Fn);
2432+
return F->hasPrefixData();
2433+
}
2434+
2435+
void LLVMSetPrefixData(LLVMValueRef Fn, LLVMValueRef prefixData) {
2436+
Function *F = unwrap<Function>(Fn);
2437+
Constant *prefix = unwrap<Constant>(prefixData);
2438+
F->setPrefixData(prefix);
2439+
}
2440+
2441+
LLVMValueRef LLVMGetPrologueData(LLVMValueRef Fn) {
2442+
Function *F = unwrap<Function>(Fn);
2443+
return wrap(F->getPrologueData());
2444+
}
2445+
2446+
LLVMBool LLVMHasPrologueData(LLVMValueRef Fn) {
2447+
Function *F = unwrap<Function>(Fn);
2448+
return F->hasPrologueData();
2449+
}
2450+
2451+
void LLVMSetPrologueData(LLVMValueRef Fn, LLVMValueRef prologueData) {
2452+
Function *F = unwrap<Function>(Fn);
2453+
Constant *prologue = unwrap<Constant>(prologueData);
2454+
F->setPrologueData(prologue);
2455+
}
2456+
24252457
void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
24262458
LLVMAttributeRef A) {
24272459
unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));

llvm/test/Bindings/llvm-c/echo.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,20 @@ define void @test_fast_math_flags_call_outer(float %a) {
334334
ret void
335335
}
336336

337+
define void @test_func_prefix_data_01() prefix i32 123 {
338+
ret void
339+
}
340+
341+
define void @test_func_prefix_data_02() prefix i64 2000 {
342+
ret void
343+
}
344+
345+
%func_prolog_struct = type <{ i8, i8, ptr }>
346+
347+
define void @test_func_prologue_data_01() prologue %func_prolog_struct <{ i8 235, i8 8, ptr zeroinitializer}> {
348+
ret void
349+
}
350+
337351
!llvm.dbg.cu = !{!0, !2}
338352
!llvm.module.flags = !{!3}
339353

llvm/tools/llvm-c-test/echo.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,14 @@ static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
13971397
}
13981398
LLVMDisposeValueMetadataEntries(AllMetadata);
13991399

1400+
// Copy any prefix data that may be on the function
1401+
if (LLVMHasPrefixData(Cur))
1402+
LLVMSetPrefixData(Fun, clone_constant(LLVMGetPrefixData(Cur), M));
1403+
1404+
// Copy any prologue data that may be on the function
1405+
if (LLVMHasPrologueData(Cur))
1406+
LLVMSetPrologueData(Fun, clone_constant(LLVMGetPrologueData(Cur), M));
1407+
14001408
FunCloner FC(Cur, Fun);
14011409
FC.CloneBBs(Cur);
14021410

0 commit comments

Comments
 (0)