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;
3233DEFINE_SIMPLE_CONVERSION_FUNCTIONS (ObjectLayer, LLVMOrcObjectLayerRef)
3334DEFINE_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
204247std::string OrcCAPITestBase::TargetTriple;
205248bool 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.
235254static std::string toString (LLVMErrorRef E) {
0 commit comments