Skip to content

Commit 52e62c6

Browse files
committed
[CodeGen] Allow mixed scalar type constraints for inline asm
GCC supports code like "asm volatile ("" : "=r" (i) : "0" (f))" where i is integer type and f is floating point type. Currently this code produces an error with Clang. The change allows mixed scalar types between input and output constraints.
1 parent b7d911b commit 52e62c6

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9322,9 +9322,11 @@ static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
93229322
std::pair<unsigned, const TargetRegisterClass *> InputRC =
93239323
TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
93249324
MatchingOpInfo.ConstraintVT);
9325-
if ((OpInfo.ConstraintVT.isInteger() !=
9326-
MatchingOpInfo.ConstraintVT.isInteger()) ||
9327-
(MatchRC.second != InputRC.second)) {
9325+
const bool OutOpIsIntOrFP =
9326+
OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9327+
const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9328+
MatchingOpInfo.ConstraintVT.isFloatingPoint();
9329+
if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
93289330
// FIXME: error out in a more elegant fashion
93299331
report_fatal_error("Unsupported asm: input constraint"
93309332
" with a matching output constraint of"

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5824,8 +5824,11 @@ TargetLowering::ParseConstraints(const DataLayout &DL,
58245824
std::pair<unsigned, const TargetRegisterClass *> InputRC =
58255825
getRegForInlineAsmConstraint(TRI, Input.ConstraintCode,
58265826
Input.ConstraintVT);
5827-
if ((OpInfo.ConstraintVT.isInteger() !=
5828-
Input.ConstraintVT.isInteger()) ||
5827+
const bool OutOpIsIntOrFP = OpInfo.ConstraintVT.isInteger() ||
5828+
OpInfo.ConstraintVT.isFloatingPoint();
5829+
const bool InOpIsIntOrFP = Input.ConstraintVT.isInteger() ||
5830+
Input.ConstraintVT.isFloatingPoint();
5831+
if ((OutOpIsIntOrFP != InOpIsIntOrFP) ||
58295832
(MatchRC.second != InputRC.second)) {
58305833
report_fatal_error("Unsupported asm: input constraint"
58315834
" with a matching output constraint of"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc -mtriple=x86_64-unknown-linux-gnu -mattr +avx < %s | FileCheck %s
3+
4+
; The C source used as a base for generating this test:.
5+
6+
; unsigned test(float f)
7+
; {
8+
; unsigned i;
9+
; // Copies f into the output operand i
10+
; asm volatile ("" : "=r" (i) : "0" (f));
11+
; return i;
12+
; }
13+
14+
15+
define i32 @test_int_float(float %f) {
16+
; CHECK-LABEL: test_int_float:
17+
; CHECK: # %bb.0: # %entry
18+
; CHECK-NEXT: vmovd %xmm0, %eax
19+
; CHECK-NEXT: #APP
20+
; CHECK-NEXT: #NO_APP
21+
; CHECK-NEXT: retq
22+
entry:
23+
%asm_call = call i32 asm sideeffect "", "=r,0,~{dirflag},~{fpsr},~{flags}"(float %f)
24+
ret i32 %asm_call
25+
}
26+
27+
define i32 @test_int_ptr(ptr %f) {
28+
; CHECK-LABEL: test_int_ptr:
29+
; CHECK: # %bb.0: # %entry
30+
; CHECK-NEXT: movq %rdi, %rax
31+
; CHECK-NEXT: #APP
32+
; CHECK-NEXT: #NO_APP
33+
; CHECK-NEXT: # kill: def $eax killed $eax killed $rax
34+
; CHECK-NEXT: retq
35+
entry:
36+
%asm_call = call i32 asm sideeffect "", "=r,0,~{dirflag},~{fpsr},~{flags}"(ptr %f)
37+
ret i32 %asm_call
38+
}
39+
40+
define i64 @test_int_vec(<4 x i16> %v) {
41+
; CHECK-LABEL: test_int_vec:
42+
; CHECK: # %bb.0: # %entry
43+
; CHECK-NEXT: #APP
44+
; CHECK-NEXT: #NO_APP
45+
; CHECK-NEXT: vmovq %xmm0, %rax
46+
; CHECK-NEXT: retq
47+
entry:
48+
%asm_call = call i64 asm sideeffect "", "=v,0,~{dirflag},~{fpsr},~{flags}"(<4 x i16> %v)
49+
ret i64 %asm_call
50+
}
51+
52+
define <4 x i32> @test_int_vec_float_vec(<4 x float> %f) {
53+
; CHECK-LABEL: test_int_vec_float_vec:
54+
; CHECK: # %bb.0: # %entry
55+
; CHECK-NEXT: #APP
56+
; CHECK-NEXT: #NO_APP
57+
; CHECK-NEXT: retq
58+
entry:
59+
%asm_call = call <4 x i32> asm sideeffect "", "=v,0,~{dirflag},~{fpsr},~{flags}"(<4 x float> %f)
60+
ret <4 x i32> %asm_call
61+
}

0 commit comments

Comments
 (0)