Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell clang that my LLVM Target should use 16-bit 'int'?

For my PIC Backend, I want 'int' to be 16 bits. How can I / my target tell clang what should be the size of 'int'? Defining 16-bit registers only seems not sufficient.

Currently "clang -O2 -emit-llvm -target pic" converts

int foo(int a, int b) { return a + b; }

to this IR code, using 32-bit integers:

; ModuleID = '../test/sum.c'
source_filename = "../test/sum.c"
target datalayout = "e-m:e-p:16:16-i16:16-a:0:16-n16-S16"
target triple = "pic"

; Function Attrs: norecurse nounwind readnone
define i32 @foo(i32 %a, i32 %b) local_unnamed_addr #0 {
entry:
  %add = add nsw i32 %b, %a
  ret i32 %add
}

attributes #0 = { norecurse nounwind readnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = !{!"clang version 4.0.0 (http://llvm.org/git/clang.git 92920e1616528c259756dd8190d4a47058fae127) (http://llvm.org/git/llvm.git 7ca31361200d6bc8a75fa06f112083a8be544287)"}

This may or may not be the cause of the "Return operand #1 has unhandled type i16" message I described in PIC Backend: 16-bit registers / return type. However I should probably get the type used in clang's output correct before turning to other problems.

like image 857
Maximilian Rixius Avatar asked Nov 15 '25 06:11

Maximilian Rixius


1 Answers

SOLVED: clang takes the size of int (and other types) from its own Target, defined in clang/lib/Basics/Targets.cpp. The native size setting "-n16" is not sufficient to override the (default?) i32 setting. Instead:

IntWidth = 32;
IntAlign = 32;

in my target's constructor does the trick.

This also solves the strange 'unhandled return type i16' issue. Don't know why.

like image 52
Maximilian Rixius Avatar answered Nov 17 '25 20:11

Maximilian Rixius