Skip to content

[C API] Add accessors for function prefix and prologue data #82193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ Changes to the C API
* Added ``LLVMConstStringInContext2`` function, which better matches the C++
API by using ``size_t`` for string length. Deprecated ``LLVMConstStringInContext``.

* Added the following functions for accessing a function's prefix data:

* ``LLVMHasPrefixData``
* ``LLVMGetPrefixData``
* ``LLVMSetPrefixData``

* Added the following functions for accessing a function's prologue data:

* ``LLVMHasPrologueData``
* ``LLVMGetPrologueData``
* ``LLVMSetPrologueData``

Changes to the CodeGen infrastructure
-------------------------------------

Expand Down
38 changes: 38 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2730,6 +2730,44 @@ const char *LLVMGetGC(LLVMValueRef Fn);
*/
void LLVMSetGC(LLVMValueRef Fn, const char *Name);

/**
* Gets the prefix data associated with a function. Only valid on functions, and
* only if LLVMHasPrefixData returns true.
* See https://llvm.org/docs/LangRef.html#prefix-data
*/
LLVMValueRef LLVMGetPrefixData(LLVMValueRef Fn);

/**
* Check if a given function has prefix data. Only valid on functions.
* See https://llvm.org/docs/LangRef.html#prefix-data
*/
LLVMBool LLVMHasPrefixData(LLVMValueRef Fn);

/**
* Sets the prefix data for the function. Only valid on functions.
* See https://llvm.org/docs/LangRef.html#prefix-data
*/
void LLVMSetPrefixData(LLVMValueRef Fn, LLVMValueRef prefixData);

/**
* Gets the prologue data associated with a function. Only valid on functions,
* and only if LLVMHasPrologueData returns true.
* See https://llvm.org/docs/LangRef.html#prologue-data
*/
LLVMValueRef LLVMGetPrologueData(LLVMValueRef Fn);

/**
* Check if a given function has prologue data. Only valid on functions.
* See https://llvm.org/docs/LangRef.html#prologue-data
*/
LLVMBool LLVMHasPrologueData(LLVMValueRef Fn);

/**
* Sets the prologue data for the function. Only valid on functions.
* See https://llvm.org/docs/LangRef.html#prologue-data
*/
void LLVMSetPrologueData(LLVMValueRef Fn, LLVMValueRef prologueData);

/**
* Add an attribute to a function.
*
Expand Down
32 changes: 32 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,38 @@ void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
F->clearGC();
}

LLVMValueRef LLVMGetPrefixData(LLVMValueRef Fn) {
Function *F = unwrap<Function>(Fn);
return wrap(F->getPrefixData());
}

LLVMBool LLVMHasPrefixData(LLVMValueRef Fn) {
Function *F = unwrap<Function>(Fn);
return F->hasPrefixData();
}

void LLVMSetPrefixData(LLVMValueRef Fn, LLVMValueRef prefixData) {
Function *F = unwrap<Function>(Fn);
Constant *prefix = unwrap<Constant>(prefixData);
F->setPrefixData(prefix);
}

LLVMValueRef LLVMGetPrologueData(LLVMValueRef Fn) {
Function *F = unwrap<Function>(Fn);
return wrap(F->getPrologueData());
}

LLVMBool LLVMHasPrologueData(LLVMValueRef Fn) {
Function *F = unwrap<Function>(Fn);
return F->hasPrologueData();
}

void LLVMSetPrologueData(LLVMValueRef Fn, LLVMValueRef prologueData) {
Function *F = unwrap<Function>(Fn);
Constant *prologue = unwrap<Constant>(prologueData);
F->setPrologueData(prologue);
}

void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
LLVMAttributeRef A) {
unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/Bindings/llvm-c/echo.ll
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,20 @@ define void @test_fast_math_flags_call_outer(float %a) {
ret void
}

define void @test_func_prefix_data_01() prefix i32 123 {
ret void
}

define void @test_func_prefix_data_02() prefix i64 2000 {
ret void
}

%func_prolog_struct = type <{ i8, i8, ptr }>

define void @test_func_prologue_data_01() prologue %func_prolog_struct <{ i8 235, i8 8, ptr zeroinitializer}> {
ret void
}

!llvm.dbg.cu = !{!0, !2}
!llvm.module.flags = !{!3}

Expand Down
8 changes: 8 additions & 0 deletions llvm/tools/llvm-c-test/echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,14 @@ static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
}
LLVMDisposeValueMetadataEntries(AllMetadata);

// Copy any prefix data that may be on the function
if (LLVMHasPrefixData(Cur))
LLVMSetPrefixData(Fun, clone_constant(LLVMGetPrefixData(Cur), M));

// Copy any prologue data that may be on the function
if (LLVMHasPrologueData(Cur))
LLVMSetPrologueData(Fun, clone_constant(LLVMGetPrologueData(Cur), M));

FunCloner FC(Cur, Fun);
FC.CloneBBs(Cur);

Expand Down