[llvm] [C API] Add accessors for function prefix and prologue data (PR #82193)
Benji Smith via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 17 17:41:06 PDT 2024
https://github.com/Benjins updated https://github.com/llvm/llvm-project/pull/82193
>From b6c6cc255272c25435c1ef3b61bdef6ff8d22b47 Mon Sep 17 00:00:00 2001
From: Benji Smith <benjsith at gmail.com>
Date: Sat, 17 Feb 2024 10:31:36 -0500
Subject: [PATCH] [C API] Add accessors for function prefix and prologue data
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
---
llvm/docs/ReleaseNotes.rst | 12 ++++++++++
llvm/include/llvm-c/Core.h | 38 +++++++++++++++++++++++++++++++
llvm/lib/IR/Core.cpp | 32 ++++++++++++++++++++++++++
llvm/test/Bindings/llvm-c/echo.ll | 14 ++++++++++++
llvm/tools/llvm-c-test/echo.cpp | 8 +++++++
5 files changed, 104 insertions(+)
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 7be51730663bd1..5e5b94255359b2 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -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
-------------------------------------
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 7cfe4dc4f775fd..10e65c3d7724b5 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -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.
*
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 4b804a41a1676f..8baf2ec14f10d2 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -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));
diff --git a/llvm/test/Bindings/llvm-c/echo.ll b/llvm/test/Bindings/llvm-c/echo.ll
index be0207599478b8..953a16b7e624e1 100644
--- a/llvm/test/Bindings/llvm-c/echo.ll
+++ b/llvm/test/Bindings/llvm-c/echo.ll
@@ -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}
diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp
index bc708e2d472edd..d129fce3e8875c 100644
--- a/llvm/tools/llvm-c-test/echo.cpp
+++ b/llvm/tools/llvm-c-test/echo.cpp
@@ -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, LLVMGetPrefixData(Cur));
+
+ // Copy any prologue data that may be on the function
+ if (LLVMHasPrologueData(Cur))
+ LLVMSetPrologueData(Fun, LLVMGetPrologueData(Cur));
+
FunCloner FC(Cur, Fun);
FC.CloneBBs(Cur);
More information about the llvm-commits
mailing list