Skip to content

Commit

Permalink
LLVM 9.0.x support (ponylang#3320)
Browse files Browse the repository at this point in the history
- Update to allow building with LLVM 9.0.x.
- Update the vendored LLVM version to 9.0.1

Thanks very much to @mshockwave for their help!
  • Loading branch information
chalcolith authored and SeanTAllen committed Dec 23, 2019
1 parent 1fec8bf commit 0fea9f8
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 34 deletions.
6 changes: 4 additions & 2 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ branches:

environment:
matrix:
- llvm: 7.0.1
- llvm: 7.1.0
- llvm: 8.0.1
- llvm: 9.0.0

configuration:
- release
Expand Down Expand Up @@ -67,7 +69,7 @@ artifacts:
- path: 'ponyc-*.zip'

deploy:
# On branche `release`, deploy (and publish) artifacts
# On branch `release`, deploy (and publish) artifacts
# to the ponyc-win projects on Bintray.
- provider: BinTray
username: pony-buildbot-2
Expand Down
3 changes: 2 additions & 1 deletion Makefile-ponyc
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,10 @@ ifneq ($(MAKECMDGOALS),clean)
else ifeq ($(llvm_version),5.0.2)
else ifeq ($(llvm_version),6.0.1)
else ifeq ($(llvm_version),7.1.0)
else ifeq ($(llvm_version),9.0.1)
else
$(warning WARNING: Unsupported LLVM version: $(llvm_version))
$(warning Please use LLVM 3.9.1, 5.0.2, 6.0.1, 7.1.0)
$(warning Please use LLVM 3.9.1, 5.0.2, 6.0.1, 7.1.0, 9.0.1)
endif

# Third party, but prebuilt. Prebuilt libraries are defined as
Expand Down
2 changes: 1 addition & 1 deletion lib/llvm/src
Submodule src updated 47791 files
17 changes: 16 additions & 1 deletion src/libponyc/codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,11 @@ void codegen_startfun(compile_t* c, LLVMValueRef fun, LLVMMetadataRef file,
LLVMPositionBuilderAtEnd(c->builder, block);
}

#if PONY_LLVM < 900
LLVMSetCurrentDebugLocation2(c->builder, 0, 0, NULL);
#else
LLVMSetCurrentDebugLocation2(c->builder, NULL);
#endif
}

void codegen_finishfun(compile_t* c)
Expand Down Expand Up @@ -1217,12 +1221,23 @@ void codegen_poptry(compile_t* c)

void codegen_debugloc(compile_t* c, ast_t* ast)
{
if(ast != NULL)
if(ast != NULL && c->frame->di_scope != NULL)
{
#if PONY_LLVM < 900
LLVMSetCurrentDebugLocation2(c->builder,
(unsigned)ast_line(ast), (unsigned)ast_pos(ast), c->frame->di_scope);
#else
LLVMMetadataRef loc = LLVMDIBuilderCreateDebugLocation(c->context,
(unsigned)ast_line(ast), (unsigned)ast_pos(ast), c->frame->di_scope,
NULL);
LLVMSetCurrentDebugLocation2(c->builder, loc);
#endif
} else {
#if PONY_LLVM < 900
LLVMSetCurrentDebugLocation2(c->builder, 0, 0, NULL);
#else
LLVMSetCurrentDebugLocation2(c->builder, NULL);
#endif
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/libponyc/codegen/gendebug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,25 @@ LLVMMetadataRef LLVMDIBuilderCreateMethod(LLVMDIBuilderRef d,
DIBuilder* pd = unwrap(d);
Function* f = unwrap<Function>(func);

#if PONY_LLVM >= 400
#if PONY_LLVM >= 800
DISubprogram::DISPFlags sp_flags = DISubprogram::toSPFlags(false, true,
optimized ? true : false);

DISubprogram* di_method = pd->createMethod(unwrap<DIScope>(scope),
name, linkage, unwrap<DIFile>(file), line, unwrap<DISubroutineType>(type),
0, 0, nullptr, DINode::FlagZero, sp_flags, nullptr, nullptr);
#elif PONY_LLVM >= 400
DISubprogram* di_method = pd->createMethod(unwrap<DIScope>(scope),
name, linkage, unwrap<DIFile>(file), line, unwrap<DISubroutineType>(type),
false, true, 0, 0, 0, nullptr, DINode::FlagZero, optimized ? true : false);

f->setSubprogram(di_method);
return wrap(di_method);
#else
DISubprogram* di_method = pd->createMethod(unwrap<DIScope>(scope),
name, linkage, unwrap<DIFile>(file), line, unwrap<DISubroutineType>(type),
false, true, 0, 0, 0, 0, optimized);
#endif

f->setSubprogram(di_method);
return wrap(di_method);
#endif
}

LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(LLVMDIBuilderRef d,
Expand Down
4 changes: 2 additions & 2 deletions src/libponyc/codegen/gendebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ LLVMMetadataRef LLVMDIBuilderCreateArtificialVariable(LLVMDIBuilderRef d,
LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef d,
const char* name, uint64_t size_bits, uint64_t align_bits,
unsigned encoding);
#endif

#if PONY_LLVM < 700
LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef d,
LLVMMetadataRef elem_type, uint64_t size_bits, uint64_t align_bits);
#endif
Expand Down Expand Up @@ -137,8 +135,10 @@ LLVMValueRef LLVMDIBuilderInsertDeclare(LLVMDIBuilderRef d,
LLVMValueRef value, LLVMMetadataRef info, LLVMMetadataRef expr,
unsigned line, unsigned col, LLVMMetadataRef scope, LLVMBasicBlockRef block);

#if PONY_LLVM < 900
void LLVMSetCurrentDebugLocation2(LLVMBuilderRef b,
unsigned line, unsigned col, LLVMMetadataRef scope);
#endif

PONY_EXTERN_C_END

Expand Down
96 changes: 91 additions & 5 deletions src/libponyc/codegen/genjit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# include <llvm/ExecutionEngine/Orc/CompileUtils.h>
# include <llvm/ExecutionEngine/Orc/Core.h>
# include <llvm/ExecutionEngine/Orc/Legacy.h>
# include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
# include <llvm/ExecutionEngine/Orc/IRCompileLayer.h>
# include <llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h>
# include <llvm/IR/DataLayout.h>
Expand All @@ -22,6 +23,77 @@
using namespace llvm;
using namespace llvm::orc;

#if PONY_LLVM >= 800

class PonyJIT
{
ExecutionSession _es;
RTDyldObjectLinkingLayer _obj_layer;
IRCompileLayer _compile_layer;

DataLayout _dl;
MangleAndInterner _mangle;
ThreadSafeContext _ctx;

public:
PonyJIT(JITTargetMachineBuilder jtmb, DataLayout dl) :
_es(),
_obj_layer(_es, []() { return llvm::make_unique<SectionMemoryManager>();}),
_compile_layer(_es, _obj_layer, ConcurrentIRCompiler(std::move(jtmb))),
_dl(std::move(dl)),
_mangle(_es, _dl),
_ctx(llvm::make_unique<LLVMContext>())
{
#if PONY_LLVM < 900
_es.getMainJITDylib().setGenerator(
cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(_dl)));
#else
_es.getMainJITDylib().setGenerator(
cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
_dl.getGlobalPrefix())));
#endif

#if (PONY_LLVM >= 800) && defined(PLATFORM_IS_WINDOWS)
_obj_layer.setOverrideObjectFlagsWithResponsibilityFlags(true);
#endif
}

static Expected<std::unique_ptr<PonyJIT>> create()
{
auto jtmb = JITTargetMachineBuilder::detectHost();
if (!jtmb)
return jtmb.takeError();

auto dl = jtmb->getDefaultDataLayoutForTarget();
if (!dl)
return dl.takeError();

return llvm::make_unique<PonyJIT>(std::move(*jtmb), std::move(*dl));
}

Error addModule(std::unique_ptr<Module> module)
{
return _compile_layer.add(_es.getMainJITDylib(),
ThreadSafeModule(std::move(module), _ctx));
}

Expected<JITEvaluatedSymbol> lookup(StringRef name)
{
return _es.lookup({&_es.getMainJITDylib()}, _mangle(name.str()));
}

JITTargetAddress getSymbolAddress(const char* name)
{
auto symbol = lookup(name);
if (!symbol)
return 0;

return symbol->getAddress();
}
};

#else

class PonyJIT
{
ExecutionSession _es;
Expand Down Expand Up @@ -87,6 +159,7 @@ class PonyJIT
return cantFail(symbol.getAddress());
}
};
#endif

bool gen_jit_and_run(compile_t* c, int* exit_code, jit_symbol_t* symbols,
size_t symbol_count)
Expand All @@ -105,27 +178,40 @@ bool gen_jit_and_run(compile_t* c, int* exit_code, jit_symbol_t* symbols,
if (LLVMVerifyModule(c->module, LLVMReturnStatusAction, NULL) != 0)
return false;

PonyJIT jit;
jit.addModule(std::unique_ptr<Module>(unwrap(c->module)));
#if PONY_LLVM >= 800
auto jit = cantFail(PonyJIT::create());

auto err = jit->addModule(std::unique_ptr<Module>(unwrap(c->module)));
if (err)
{
errorf(c->opt->check.errors, nullptr, "LLVM ORC JIT Error");
return false;
}
c->module = nullptr;

if (jit.error)
#else
auto jit = llvm::make_unique<PonyJIT>();
jit->addModule(std::unique_ptr<Module>(llvm::unwrap(c->module)));
c->module = nullptr;

if (jit->error)
{
errorf(c->opt->check.errors, nullptr, "LLVM ORC JIT Error");
return false;
}
#endif

for (size_t i = 0; i < symbol_count; i++)
{
void* address = (void*)jit.getSymbolAddress(symbols[i].name);
void* address = (void*)jit->getSymbolAddress(symbols[i].name);
if (address == nullptr)
return false;

memcpy(symbols[i].address, address, symbols[i].size);
}

auto main = reinterpret_cast<int(*)(int, const char**, const char**)>(
jit.getSymbolAddress("main")
jit->getSymbolAddress("main")
);

if (main == nullptr)
Expand Down
9 changes: 9 additions & 0 deletions src/libponyc/codegen/genopt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ class HeapToStack : public FunctionPass
CallGraphNode *cg_node = cg ? (*cg)[&f] : nullptr;
if (cg_node)
{
#if PONY_LLVM < 900
cg_node->removeCallEdgeFor(call);
#else
CallInst *callInst = cast<CallInst>(call.getInstruction());
cg_node->removeCallEdgeFor(*callInst);
#endif
}
#endif
inst->eraseFromParent();
Expand Down Expand Up @@ -427,7 +432,11 @@ class HeapToStack : public FunctionPass
continue;
}

#if PONY_LLVM >= 800
Instruction* term = bb->getTerminator();
#else
TerminatorInst* term = bb->getTerminator();
#endif
unsigned count = term->getNumSuccessors();

for(unsigned i = 0; i < count; i++)
Expand Down
24 changes: 24 additions & 0 deletions src/libponyc/codegen/genprim.c
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,29 @@ static void make_rdtscp(compile_t* c)
{
if(target_is_x86(c->opt->triple))
{
#if PONY_LLVM >= 800
// { i64, i32 } @llvm.x86.rdtscp()
LLVMTypeRef r_type_fields[2] = { c->i64, c->i32 };
LLVMTypeRef r_type = LLVMStructTypeInContext(c->context, r_type_fields, 2,
true);
LLVMTypeRef f_type = LLVMFunctionType(r_type, NULL, 0, false);
LLVMValueRef rdtscp = LLVMAddFunction(c->module, "llvm.x86.rdtscp", f_type);

// i64 @internal.x86.rdtscp(i32*)
LLVMTypeRef i32_ptr = LLVMPointerType(c->i32, 0);
f_type = LLVMFunctionType(c->i64, &i32_ptr, 1, false);
LLVMValueRef fun = codegen_addfun(c, "internal.x86.rdtscp", f_type, false);
LLVMSetFunctionCallConv(fun, LLVMCCallConv);

codegen_startfun(c, fun, NULL, NULL, NULL, false);
LLVMValueRef result = LLVMBuildCall(c->builder, rdtscp, NULL, 0, "");
LLVMValueRef second = LLVMBuildExtractValue(c->builder, result, 1, "");
LLVMValueRef argptr = LLVMGetParam(fun, 0);
LLVMBuildStore(c->builder, second, argptr);
LLVMValueRef first = LLVMBuildExtractValue(c->builder, result, 0, "");
LLVMBuildRet(c->builder, first);
codegen_finishfun(c);
#else
// i64 @llvm.x86.rdtscp(i8*)
LLVMTypeRef f_type = LLVMFunctionType(c->i64, &c->void_ptr, 1, false);
LLVMValueRef rdtscp = LLVMAddFunction(c->module, "llvm.x86.rdtscp",
Expand All @@ -1951,6 +1974,7 @@ static void make_rdtscp(compile_t* c)
LLVMBuildRet(c->builder, result);

codegen_finishfun(c);
#endif
} else {
(void)c;
}
Expand Down
6 changes: 5 additions & 1 deletion src/libponyc/codegen/gentype.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ static void make_debug_basic(compile_t* c, reach_type_t* t)
encoding = DW_ATE_unsigned;
}

#if PONY_LLVM >= 700
#if PONY_LLVM >= 800
(void)align;
c_t->di_type = LLVMDIBuilderCreateBasicType(c->di, t->name, strlen(t->name),
8 * size, encoding, LLVMDIFlagZero);
#elif PONY_LLVM >= 700
(void)align;
c_t->di_type = LLVMDIBuilderCreateBasicType(c->di, t->name, strlen(t->name),
8 * size, encoding);
Expand Down
3 changes: 1 addition & 2 deletions test/libponyc/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ TEST_F(CodegenTest, DoNotOptimiseApplyPrimitive)
//
// This test is disabled on LLVM 3.9 and 4.0 because exceptions crossing JIT
// boundaries are broken with the ORC JIT on these versions.
#if !defined(PLATFORM_IS_WINDOWS) && (PONY_LLVM >= 500)
#if !defined(PLATFORM_IS_WINDOWS) && (PONY_LLVM >= 500) && (PONY_LLVM < 900)
TEST_F(CodegenTest, TryBlockCantCatchCppExcept)
{
const char* src =
Expand Down Expand Up @@ -946,4 +946,3 @@ TEST_F(CodegenTest, TryThenClauseContinueNested)
ASSERT_TRUE(run_program(&exit_code));
ASSERT_EQ(exit_code, 42);
}

Loading

0 comments on commit 0fea9f8

Please sign in to comment.