Skip to content
Merged
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
71 changes: 45 additions & 26 deletions llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "llvm-c/Orc.h"
#include "gtest/gtest.h"

#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
Expand All @@ -32,6 +33,20 @@ using namespace llvm::orc;
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectLayer, LLVMOrcObjectLayerRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeModule, LLVMOrcThreadSafeModuleRef)

// A class that sets strings for extension attributes by querying
// TargetLibraryInfo.
struct TargetI32ArgExtensions {
std::string Ret;
std::string Arg;
TargetI32ArgExtensions(std::string TargetTriple, bool Signed = true) {
Triple T(TargetTriple);
if (auto AK = TargetLibraryInfo::getExtAttrForI32Return(T, Signed))
Ret = Attribute::getNameFromAttrKind(AK).str() + " ";
if (auto AK = TargetLibraryInfo::getExtAttrForI32Param(T, Signed))
Arg = Attribute::getNameFromAttrKind(AK).str() + " ";
}
};

// OrcCAPITestBase contains several helper methods and pointers for unit tests
// written for the LLVM-C API. It provides the following helpers:
//
Expand Down Expand Up @@ -90,6 +105,31 @@ class OrcCAPITestBase : public testing::Test {

LLVMOrcDisposeLLJIT(J);
TargetSupported = true;

// Create test functions in text format, with the proper extension
// attributes.
if (SumExample.empty()) {
TargetI32ArgExtensions ArgExt(TargetTriple);
std::ostringstream OS;
OS << "define " << ArgExt.Ret << "i32 "
<< "@sum(i32 " << ArgExt.Arg << "%x, i32 " << ArgExt.Arg << "%y)"
<< R"( {
entry:
%r = add nsw i32 %x, %y
ret i32 %r
}
)";
SumExample = OS.str();

OS << R"(
!llvm.module.flags = !{!0}
!llvm.dbg.cu = !{!1}
!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, emissionKind: FullDebug)
!2 = !DIFile(filename: "sum.c", directory: "/tmp")
)";
SumDebugExample = OS.str();
}
}

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

static std::string TargetTriple;
static bool TargetSupported;

static std::string SumExample;
static std::string SumDebugExample;
};

std::string OrcCAPITestBase::TargetTriple;
bool OrcCAPITestBase::TargetSupported = false;

namespace {

constexpr StringRef SumExample =
R"(
define i32 @sum(i32 %x, i32 %y) {
entry:
%r = add nsw i32 %x, %y
ret i32 %r
}
)";

constexpr StringRef SumDebugExample =
R"(
define i32 @sum(i32 %x, i32 %y) {
entry:
%r = add nsw i32 %x, %y
ret i32 %r
}
!llvm.module.flags = !{!0}
!llvm.dbg.cu = !{!1}
!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, emissionKind: FullDebug)
!2 = !DIFile(filename: "sum.c", directory: "/tmp")
)";

} // end anonymous namespace.
std::string OrcCAPITestBase::SumExample;
std::string OrcCAPITestBase::SumDebugExample;

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