Skip to content

Instantly share code, notes, and snippets.

@Zshazz
Last active December 15, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zshazz/c7dbd6eee0b6b242252b to your computer and use it in GitHub Desktop.
Save Zshazz/c7dbd6eee0b6b242252b to your computer and use it in GitHub Desktop.
llvm-d problem
import std.stdio, std.conv;
import llvm.all;
void main()
{
if(!LLVM.loaded)
LLVM.loadFromPath("lib");
char* error = null;
LLVMModuleRef mod = LLVMModuleCreateWithName("fac_module");
LLVMTypeRef[] fac_args = [LLVMInt32Type()];
LLVMValueRef fac = LLVMAddFunction(mod, "fac", LLVMFunctionType(LLVMInt32Type(), fac_args.ptr, 1, 0));
LLVMSetFunctionCallConv(fac, LLVMCCallConv);
LLVMValueRef n = LLVMGetParam(fac, 0);
LLVMBasicBlockRef entry = LLVMAppendBasicBlock(fac, "entry");
LLVMBasicBlockRef iftrue = LLVMAppendBasicBlock(fac, "iftrue");
LLVMBasicBlockRef iffalse = LLVMAppendBasicBlock(fac, "iffalse");
LLVMBasicBlockRef end = LLVMAppendBasicBlock(fac, "end");
LLVMBuilderRef builder = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(builder, entry);
LLVMValueRef If = LLVMBuildICmp(builder, LLVMIntEQ, n, LLVMConstInt(LLVMInt32Type(), 0, 0), "n == 0");
LLVMBuildCondBr(builder, If, iftrue, iffalse);
LLVMPositionBuilderAtEnd(builder, iftrue);
LLVMValueRef res_iftrue = LLVMConstInt(LLVMInt32Type(), 1, 0);
LLVMBuildBr(builder, end);
LLVMPositionBuilderAtEnd(builder, iffalse);
LLVMValueRef n_minus = LLVMBuildSub(builder, n, LLVMConstInt(LLVMInt32Type(), 1, 0), "n - 1");
LLVMValueRef[] call_fac_args = [n_minus];
LLVMValueRef call_fac = LLVMBuildCall(builder, fac, call_fac_args.ptr, 1, "fac(n - 1)");
LLVMValueRef res_iffalse = LLVMBuildMul(builder, n, call_fac, "n * fac(n - 1)");
LLVMBuildBr(builder, end);
LLVMPositionBuilderAtEnd(builder, end);
LLVMValueRef res = LLVMBuildPhi(builder, LLVMInt32Type(), "result");
LLVMValueRef[] phi_vals = [res_iftrue, res_iffalse];
LLVMBasicBlockRef[] phi_blocks = [iftrue, iffalse];
LLVMAddIncoming(res, phi_vals.ptr, phi_blocks.ptr, 2);
LLVMBuildRet(builder, res);
LLVMVerifyModule(mod, LLVMAbortProcessAction, &error);
LLVMDisposeMessage(error); // Handler == LLVMAbortProcessAction -> No need to check errors
LLVMExecutionEngineRef engine;
error = null;
//LLVMCreateJITCompiler(&engine, provider, 0, &error); // doesn't work yet...
LLVMCreateExecutionEngineForModule(&engine, mod, &error);
if(error) {
stderr.writeln(to!string(error));
LLVMDisposeMessage(error);
throw new Exception(to!string(error));
}
LLVMPassManagerRef pass = LLVMCreatePassManager();
LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
LLVMAddConstantPropagationPass(pass);
LLVMAddInstructionCombiningPass(pass);
LLVMAddPromoteMemoryToRegisterPass(pass);
// LLVMAddDemoteMemoryToRegisterPass(pass); // Demotes every possible value to memory
LLVMAddGVNPass(pass);
LLVMAddCFGSimplificationPass(pass);
LLVMRunPassManager(pass, mod);
LLVMDumpModule(mod);
LLVMGenericValueRef[] exec_args = [LLVMCreateGenericValueOfInt(LLVMInt32Type(), 18, 0)];
LLVMGenericValueRef exec_res = LLVMRunFunction(engine, fac, 1, exec_args.ptr);
stderr.writeln("\n; Running fac...");
stderr.writeln("; Result: ", LLVMGenericValueToInt(exec_res, 0));
LLVMDisposePassManager(pass);
LLVMDisposeBuilder(builder);
LLVMDisposeExecutionEngine(engine);
}
Building configuration "application", build type debug
Running dmd (compile)...
.dub/packages/llvm-d/llvm/d/ir/type.d(86): Warning: statement is not reachable
.dub/packages/llvm-d/llvm/d/ir/type.d(346): Warning: statement is not reachable
Error: Build command failed with exit code 1
Run 'dub help' for usage information.
Building configuration "application", build type $DFLAGS
Running dmd (compile)...
.dub/packages/llvm-d/llvm/d/ir/type.d(86): Warning: statement is not reachable
.dub/packages/llvm-d/llvm/d/ir/type.d(346): Warning: statement is not reachable
Linking...
temp.o: In function `_D4llvm4util5shlib9SharedLib8loadFileMFAyaZb':
.dub/packages/llvm-d/llvm/c/constants.d:(.text._D4llvm4util5shlib9SharedLib8loadFileMFAyaZb+0x36): undefined reference to `dlopen'
.dub/packages/llvm-d/llvm/c/constants.d:(.text._D4llvm4util5shlib9SharedLib8loadFileMFAyaZb+0x4d): undefined reference to `dlerror'
temp.o: In function `_D4llvm4util5shlib9SharedLib6unloadMFZv':
.dub/packages/llvm-d/llvm/c/constants.d:(.text._D4llvm4util5shlib9SharedLib6unloadMFZv+0x60): undefined reference to `dlclose'
temp.o: In function `_D4llvm4util5shlib9SharedLib60__T10loadSymbolTPUNbPS4llvm1c5types16LLVMOpaqueModulekPPaZiZ10loadSymbolMFAyaZPUNbPS4llvm1c5types16LLVMOpaqueModulekPPaZi':
.dub/packages/llvm-d/llvm/c/constants.d:(.text._D4llvm4util5shlib9SharedLib60__T10loadSymbolTPUNbPS4llvm1c5types16LLVMOpaqueModulekPPaZiZ10loadSymbolMFAyaZPUNbPS4llvm1c5types16LLVMOpaqueModulekPPaZi+0x70): undefined reference to `dlsym'
temp.o: In function `_D4llvm4util5shlib9SharedLib56__T10loadSymbolTPUNbPS4llvm1c5types15LLVMOpaqueValuekZiZ10loadSymbolMFAyaZPUNbPS4llvm1c5types15LLVMOpaqueValuekZi':
.dub/packages/llvm-d/llvm/c/constants.d:(.text._D4llvm4util5shlib9SharedLib56__T10loadSymbolTPUNbPS4llvm1c5types15LLVMOpaqueValuekZiZ10loadSymbolMFAyaZPUNbPS4llvm1c5types15LLVMOpaqueValuekZi+0x70): undefined reference to `dlsym'
temp.o: In function `_D4llvm4util5shlib9SharedLib55__T10loadSymbolTPUNbPS4llvm1c5types15LLVMOpaqueValueZvZ10loadSymbolMFAyaZPUNbPS4llvm1c5types15LLVMOpaqueValueZv':
.dub/packages/llvm-d/llvm/c/constants.d:(.text._D4llvm4util5shlib9SharedLib55__T10loadSymbolTPUNbPS4llvm1c5types15LLVMOpaqueValueZvZ10loadSymbolMFAyaZPUNbPS4llvm1c5types15LLVMOpaqueValueZv+0x70): undefined reference to `dlsym'
temp.o: In function `_D4llvm4util5shlib9SharedLib99__T10loadSymbolTPUNbPS4llvm1c5types22LLVMOpaqueMemoryBufferPPS4llvm1c5types16LLVMOpaqueModulePPaZiZ10loadSymbolMFAyaZPUNbPS4llvm1c5types22LLVMOpaqueMemoryBufferPPS4llvm1c5types16LLVMOpaqueModulePPaZi':
.dub/packages/llvm-d/llvm/c/constants.d:(.text._D4llvm4util5shlib9SharedLib99__T10loadSymbolTPUNbPS4llvm1c5types22LLVMOpaqueMemoryBufferPPS4llvm1c5types16LLVMOpaqueModulePPaZiZ10loadSymbolMFAyaZPUNbPS4llvm1c5types22LLVMOpaqueMemoryBufferPPS4llvm1c5types16LLVMOpaqueModulePPaZi+0x70): undefined reference to `dlsym'
temp.o: In function `_D4llvm4util5shlib9SharedLib133__T10loadSymbolTPUNbPS4llvm1c5types17LLVMOpaqueContextPS4llvm1c5types22LLVMOpaqueMemoryBufferPPS4llvm1c5types16LLVMOpaqueModulePPaZiZ10loadSymbolMFAyaZPUNbPS4llvm1c5types17LLVMOpaqueContextPS4llvm1c5types22LLVMOpaqueMemoryBufferPPS4llvm1c5types16LLVMOpaqueModulePPaZi':
.dub/packages/llvm-d/llvm/c/constants.d:(.text._D4llvm4util5shlib9SharedLib133__T10loadSymbolTPUNbPS4llvm1c5types17LLVMOpaqueContextPS4llvm1c5types22LLVMOpaqueMemoryBufferPPS4llvm1c5types16LLVMOpaqueModulePPaZiZ10loadSymbolMFAyaZPUNbPS4llvm1c5types17LLVMOpaqueContextPS4llvm1c5types22LLVMOpaqueMemoryBufferPPS4llvm1c5types16LLVMOpaqueModulePPaZi+0x70): undefined reference to `dlsym'
temp.o:.dub/packages/llvm-d/llvm/c/constants.d:(.text._D4llvm4util5shlib9SharedLib141__T10loadSymbolTPUNbPS4llvm1c5types17LLVMOpaqueContextPS4llvm1c5types22LLVMOpaqueMemoryBufferPPS4llvm1c5types24LLVMOpaqueModuleProviderPPaZiZ10loadSymbolMFAyaZPUNbPS4llvm1c5types17LLVMOpaqueContextPS4llvm1c5types22LLVMOpaqueMemoryBufferPPS4llvm1c5types24LLVMOpaqueModuleProviderPPaZi+0x70): more undefined references to `dlsym' follow
collect2: error: ld returned 1 exit status
--- errorlevel 1
Error: Link command failed with exit code 1
Starting in some directory, run these commands:
$ dub init llvmtest
$ cd llvmtest
$ (some editor) source/app.d
Paste the contents of app.d in from above, save & close
$ (some editor) package.json
Add the "llvm-d": "~master" line in dependencies
$ dub
Resulting error1 above. Dmd is treating warnings as errors... so, to overcome that:
$ export DFLAGS="-wi"
$ dub
Resulting error2 above
$ dub --rdmd
Runs correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment