2

The following lengthy C program generates a simple LLVM module containing a function that merely calls llvm.x86.sse41.round.ps. It emits the bitcode file and then runs the code generated by LLVM. My question is how do I find out target triple and instruction extensions like SSE or AVX of the host machine and how do I add this information to the LLVM module or how do I otherwise tell it to the LLVM execution engine. Here is, what I do:

$ cat ctest/avx-instruction-selection.c
#include <llvm-c/Core.h>
#include <llvm-c/Target.h>
#include <llvm-c/ExecutionEngine.h>
#include <llvm-c/BitWriter.h>
#include <llvm-c/Transforms/Scalar.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#if 1
const int vectorSize = 4;
const char* roundName = "llvm.x86.sse41.round.ps";
#else
const int vectorSize = 8;
const char* roundName = "llvm.x86.avx.round.ps.256";
#endif

int main ()
{
  LLVMModuleRef module;
  LLVMExecutionEngineRef execEngine;
  LLVMTargetDataRef targetData;
  LLVMTypeRef floatType, vectorType, ptrType, voidType, funcType, roundType, int32Type;
  LLVMValueRef func, roundFunc;
  LLVMValueRef param, loaded, const1, callRound;
  LLVMBuilderRef builder;
  LLVMBasicBlockRef block;
  const int false = 0;

  LLVMInitializeX86TargetInfo();
  LLVMInitializeX86Target();
  LLVMInitializeX86TargetMC();
  module = LLVMModuleCreateWithName("_module");
  LLVMSetTarget(module, "x86_64-unknown-linux-gnu");
  floatType = LLVMFloatType();
  vectorType = LLVMVectorType(floatType, vectorSize);
  ptrType = LLVMPointerType(vectorType, 0);
  voidType = LLVMVoidType();
  LLVMTypeRef roundParams[] = { ptrType };
  roundType = LLVMFunctionType(voidType, roundParams, 1, false);
  func = LLVMAddFunction(module, "round", roundType);
  LLVMSetLinkage(func, LLVMExternalLinkage);
  builder = LLVMCreateBuilder();
  block = LLVMAppendBasicBlock(func, "_L1");
  LLVMPositionBuilderAtEnd(builder, block);
  param = LLVMGetParam(func, 0);
  loaded = LLVMBuildLoad(builder, param, "");
  int32Type = LLVMIntType(32);
  LLVMTypeRef funcParams[] = { vectorType, int32Type } ;
  funcType = LLVMFunctionType(vectorType, funcParams, 2, false);
  roundFunc = LLVMAddFunction(module, roundName, funcType);
  LLVMSetLinkage(roundFunc, LLVMExternalLinkage);
  const1 = LLVMConstInt(int32Type, 1, false);
  LLVMValueRef callParams [] = { loaded, const1 } ;
  callRound = LLVMBuildCall(builder, roundFunc, callParams, 2, "");
  LLVMSetInstructionCallConv(callRound, 0);
  LLVMAddInstrAttribute(callRound, 0, 0);
  LLVMBuildStore(builder, callRound, param);
  LLVMBuildRetVoid(builder);
  LLVMWriteBitcodeToFile(module, "round-avx.bc");
  char *errorMsg;
  LLVMCreateExecutionEngineForModule(&execEngine, module, &errorMsg);
  targetData = LLVMGetExecutionEngineTargetData(execEngine);
  size_t vectorSize0 = LLVMStoreSizeOfType(targetData, vectorType);
  size_t vectorAlign = LLVMABIAlignmentOfType(targetData, vectorType);
  float vector[vectorSize];
  printf("%lx, size %lx, align %lx\n", (size_t)vector, vectorSize0, vectorAlign);
  LLVMGenericValueRef genericVector = LLVMCreateGenericValueOfPointer(vector);
  LLVMGenericValueRef runParams[] = { genericVector } ;
  LLVMRunFunction(execEngine, func, 1, runParams);
  return 0;
}

$ gcc -Wall -o ctest/avx-instruction-selection ctest/avx-instruction-selection.c `/usr/lib/llvm-3.4/bin/llvm-config --cflags --ldflags` -lLLVM-3.4

$ ctest/avx-instruction-selection
7fff590431c0, size 10, align 10

$ ls round-avx.bc
round-avx.bc

$ llvm-dis -o - round-avx.bc
; ModuleID = 'round-avx.bc'
target triple = "x86_64-unknown-linux-gnu"

define void @round(<4 x float>*) {
_L1:
  %1 = load <4 x float>* %0
  %2 = call <4 x float> @llvm.x86.sse41.round.ps(<4 x float> %1, i32 1)
  store <4 x float> %2, <4 x float>* %0
  ret void
}

; Function Attrs: nounwind readnone
declare <4 x float> @llvm.x86.sse41.round.ps(<4 x float>, i32) #0

attributes #0 = { nounwind readnone }

$ gcc -Wall -o ctest/avx-instruction-selection ctest/avx-instruction-selection.c `/usr/lib/llvm-3.5/bin/llvm-config --cflags --ldflags` -lLLVM-3.5

$ ctest/avx-instruction-selection
7ffed6170350, size 10, align 10
LLVM ERROR: Cannot select: intrinsic %llvm.x86.sse41.round.ps

$ gcc -Wall -o ctest/avx-instruction-selection ctest/avx-instruction-selection.c `/usr/lib/llvm-3.6/bin/llvm-config --cflags --ldflags` -lLLVM-3.6

$ ctest/avx-instruction-selection
7ffeae91eb40, size 10, align 10
LLVM ERROR: Target does not support MC emission!

$ gcc -Wall -o ctest/avx-instruction-selection ctest/avx-instruction-selection.c `/usr/lib/llvm-3.7/bin/llvm-config --cflags --ldflags` -lLLVM-3.7

$ ctest/avx-instruction-selection
7fffb6464ea0, size 10, align 10
LLVM ERROR: Target does not support MC emission!

$ gcc -Wall -o ctest/avx-instruction-selection ctest/avx-instruction-selection.c `/usr/lib/llvm-3.8/bin/llvm-config --cflags --ldflags` -lLLVM-3.8

$ ctest/avx-instruction-selection
7ffd5e233000, size 10, align 10
LLVM ERROR: Target does not support MC emission!

Summarized: With LLVM-3.4 the example works, with LLVM-3.5 the intrinsic function round.ps cannot be found and LLVM-3.6 and later say something about MC emissions that I do not understand.

As I understand, LLVM-3.5 does not find the round.ps intrinsic and I guess that it cannot find it because I have not told it about the existing SSE extension. When running llc I can add the option -mattr=sse4.1 but how can I tell it to the execution engine?

Second question: How can I find out about the available instruction extensions like SSE of the host machine via the LLVM-C API? On x86 I can call the CPUID instruction but is there a way that works uniformly on all platforms and can LLVM assist detection of extensions?

Third question: I have hard-coded the target triple into the C code. How can I find out the host target-triple via the LLVM-C API?

Last question: What about this MC emission error?

5
  • I see that llvm-config --host-target emits the content of LLVM_DEFAULT_TARGET_TRIPLE. There are llvm/Config/config.h and llvm/Config/llvm-config.h files containing the macros LLVM_DEFAULT_TARGET_TRIPLE and LLVM_HOST_TRIPLE. They are not strictly part of the LLVM-C API but are plain C.
    – Lemming
    Commented Aug 3, 2016 at 10:36
  • If remove LLVMInitializeX86TargetInfo(), LLVMInitializeX86Target() or both, then the "MC emission" error disappears and I get consistently "Cannot select: intrinsic %llvm.x86.sse41.round.ps" on all tested LLVM versions.
    – Lemming
    Commented Aug 4, 2016 at 12:35
  • In "llvm/Support/Host.h" I found the function getHostCPUName. This should answer the question about available instruction extensions on the host machine.
    – Lemming
    Commented Aug 5, 2016 at 11:13
  • In "lib/ExecutionEngine/ExecutionEngineBindings.cpp" there is the definition of LLVMCreateExecutionEngineForModule. I copied this function to my code and added .setMCPU("corei7-avx") to the builder. However, the runtime error "Cannot select: intrinsic %llvm.x86.sse41.round.ps" remains.
    – Lemming
    Commented Aug 5, 2016 at 11:17
  • I checked how lli works and found that it additionally calls LLVMInitializeNativeAsmPrinter(); and LLVMInitializeNativeAsmParser();. The initializations LLVMInitializeX86TargetInfo() and LLVMInitializeX86Target() must remain, but the three X86Target initializations can be replaced by a single LLVMInitializeNativeTarget().
    – Lemming
    Commented Aug 6, 2016 at 6:36

1 Answer 1

1

After trying around a lot I think the answer is as follows:

Replace the lines

LLVMInitializeX86TargetInfo();
LLVMInitializeX86Target();
LLVMInitializeX86TargetMC();

by

LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
LLVMInitializeNativeAsmParser();

Replace the call of LLVMCreateExecutionEngineForModule by a call to the custom function LLVMCreateExecutionEngineForModuleCPU. It is the original implementation of LLVMCreateExecutionEngineForModule plus a call of setMCPU.

#define LLVM_VERSION (LLVM_VERSION_MAJOR * 100 + LLVM_VERSION_MINOR)

LLVMBool LLVMCreateExecutionEngineForModuleCPU
    (LLVMExecutionEngineRef *OutEE,
     LLVMModuleRef M,
     char **OutError) {
  std::string Error;
#if LLVM_VERSION < 306
  EngineBuilder builder(unwrap(M));
#else
  EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
#endif
  builder.setEngineKind(EngineKind::Either)
         .setMCPU(sys::getHostCPUName().data())
         .setErrorStr(&Error);
  if (ExecutionEngine *EE = builder.create()){
    *OutEE = wrap(EE);
    return 0;
  }
  *OutError = strdup(Error.c_str());
  return 1;
}

I should also add

float vector[vectorSize] __attribute__((aligned(32)));

in order to align the array for AVX vectors.

According to an answer in the thread crash JIT with AVX intrinsics LLVMRunFunction is restricted to main-like prototypes (apparently only in MCJIT). Thus we should also replace the LLVMRunFunction stuff by

void (*funcPtr) (float *);
funcPtr = LLVMGetPointerToGlobal(execEngine, func);
funcPtr(vector);
1
  • error: ‘LLVMCreateExecutionEngineForModuleCPU’ was not declared in this scope
    – ar2015
    Commented Aug 16, 2020 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.