hatch22
hatch22

Reputation: 807

How do I initialize a constant vector in LLVM IR?

I'm trying to initialize a constant vector of ones using LLVM IR as a starting point to familiarize myself with the vector operations supported by various targets. I have the following simple main function defined:

target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc19.31.31104"

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i64 @main() #0 {  
  %one = <8 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
  %i = bitcast <8 x i8> %one to i64
  ret i64 %i
}

The line %one = <8 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1> gives a compile error from clang 13.0.1:

error: expected instruction opcode
%one = <8 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
       ^

No idea what the problem is, as the docs list in the example for the freeze instruction the following code:

; example with vectors
%v = <2 x i32> <i32 undef, i32 poison>
%a = extractelement <2 x i32> %v, i32 0    ; undef
%b = extractelement <2 x i32> %v, i32 1    ; poison
%add = add i32 %a, %a                      ; undef

which appears to use the same format for vector initialization that I'm using. I've also tried adding constant after the = sign but this yields the same error. The documentation for vector types describes constants using this format as well. For reference, I'm using the following command-line arguments with clang:

clang test.ll -o test.exe

Any idea why clang is having trouble understanding this code? I can't figure out what opcode it is expecting.

Upvotes: 3

Views: 967

Answers (1)

yugr
yugr

Reputation: 21906

Constant literals can not be assigned to variables and have to be specified directly:

%i = bitcast <8 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1> to i64

You could "assign" constant by wrapping it in a dummy operation:

%one = add <8 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>, zeroinitializer

Upvotes: 4

Related Questions