Skip to content

Commit 9f73c69

Browse files
authored
[ORC] Add signext on @sum() arguments in test. (#113308)
Make sure the inlined @sum() function has the right extension attributes on its arguments. A new struct TargetI32ArgExtensions is added that sets the Ret/Arg extension strings given a string TargetTriple. This might be used elsewhere as well for this purpose if needed. Fixes: #112503
1 parent 2d2371d commit 9f73c69

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm-c/Orc.h"
1414
#include "gtest/gtest.h"
1515

16+
#include "llvm/Analysis/TargetLibraryInfo.h"
1617
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
1718
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
1819
#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
@@ -32,6 +33,20 @@ using namespace llvm::orc;
3233
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectLayer, LLVMOrcObjectLayerRef)
3334
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeModule, LLVMOrcThreadSafeModuleRef)
3435

36+
// A class that sets strings for extension attributes by querying
37+
// TargetLibraryInfo.
38+
struct TargetI32ArgExtensions {
39+
std::string Ret;
40+
std::string Arg;
41+
TargetI32ArgExtensions(std::string TargetTriple, bool Signed = true) {
42+
Triple T(TargetTriple);
43+
if (auto AK = TargetLibraryInfo::getExtAttrForI32Return(T, Signed))
44+
Ret = Attribute::getNameFromAttrKind(AK).str() + " ";
45+
if (auto AK = TargetLibraryInfo::getExtAttrForI32Param(T, Signed))
46+
Arg = Attribute::getNameFromAttrKind(AK).str() + " ";
47+
}
48+
};
49+
3550
// OrcCAPITestBase contains several helper methods and pointers for unit tests
3651
// written for the LLVM-C API. It provides the following helpers:
3752
//
@@ -90,6 +105,31 @@ class OrcCAPITestBase : public testing::Test {
90105

91106
LLVMOrcDisposeLLJIT(J);
92107
TargetSupported = true;
108+
109+
// Create test functions in text format, with the proper extension
110+
// attributes.
111+
if (SumExample.empty()) {
112+
TargetI32ArgExtensions ArgExt(TargetTriple);
113+
std::ostringstream OS;
114+
OS << "define " << ArgExt.Ret << "i32 "
115+
<< "@sum(i32 " << ArgExt.Arg << "%x, i32 " << ArgExt.Arg << "%y)"
116+
<< R"( {
117+
entry:
118+
%r = add nsw i32 %x, %y
119+
ret i32 %r
120+
}
121+
)";
122+
SumExample = OS.str();
123+
124+
OS << R"(
125+
!llvm.module.flags = !{!0}
126+
!llvm.dbg.cu = !{!1}
127+
!0 = !{i32 2, !"Debug Info Version", i32 3}
128+
!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, emissionKind: FullDebug)
129+
!2 = !DIFile(filename: "sum.c", directory: "/tmp")
130+
)";
131+
SumDebugExample = OS.str();
132+
}
93133
}
94134

95135
void SetUp() override {
@@ -199,37 +239,16 @@ class OrcCAPITestBase : public testing::Test {
199239

200240
static std::string TargetTriple;
201241
static bool TargetSupported;
242+
243+
static std::string SumExample;
244+
static std::string SumDebugExample;
202245
};
203246

204247
std::string OrcCAPITestBase::TargetTriple;
205248
bool OrcCAPITestBase::TargetSupported = false;
206249

207-
namespace {
208-
209-
constexpr StringRef SumExample =
210-
R"(
211-
define i32 @sum(i32 %x, i32 %y) {
212-
entry:
213-
%r = add nsw i32 %x, %y
214-
ret i32 %r
215-
}
216-
)";
217-
218-
constexpr StringRef SumDebugExample =
219-
R"(
220-
define i32 @sum(i32 %x, i32 %y) {
221-
entry:
222-
%r = add nsw i32 %x, %y
223-
ret i32 %r
224-
}
225-
!llvm.module.flags = !{!0}
226-
!llvm.dbg.cu = !{!1}
227-
!0 = !{i32 2, !"Debug Info Version", i32 3}
228-
!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, emissionKind: FullDebug)
229-
!2 = !DIFile(filename: "sum.c", directory: "/tmp")
230-
)";
231-
232-
} // end anonymous namespace.
250+
std::string OrcCAPITestBase::SumExample;
251+
std::string OrcCAPITestBase::SumDebugExample;
233252

234253
// Consumes the given error ref and returns the string error message.
235254
static std::string toString(LLVMErrorRef E) {

0 commit comments

Comments
 (0)