Skip to content

Commit e9eaf19

Browse files
dfszaboarsenm
andauthored
[CodeGen] Allow mixed scalar type constraints for inline asm (#65465)
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. Co-authored-by: Matt Arsenault <[email protected]>
1 parent c1248c9 commit e9eaf19

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
@@ -9591,9 +9591,11 @@ static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
95919591
std::pair<unsigned, const TargetRegisterClass *> InputRC =
95929592
TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
95939593
MatchingOpInfo.ConstraintVT);
9594-
if ((OpInfo.ConstraintVT.isInteger() !=
9595-
MatchingOpInfo.ConstraintVT.isInteger()) ||
9596-
(MatchRC.second != InputRC.second)) {
9594+
const bool OutOpIsIntOrFP =
9595+
OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9596+
const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9597+
MatchingOpInfo.ConstraintVT.isFloatingPoint();
9598+
if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
95979599
// FIXME: error out in a more elegant fashion
95989600
report_fatal_error("Unsupported asm: input constraint"
95999601
" 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
@@ -5856,8 +5856,11 @@ TargetLowering::ParseConstraints(const DataLayout &DL,
58565856
std::pair<unsigned, const TargetRegisterClass *> InputRC =
58575857
getRegForInlineAsmConstraint(TRI, Input.ConstraintCode,
58585858
Input.ConstraintVT);
5859-
if ((OpInfo.ConstraintVT.isInteger() !=
5860-
Input.ConstraintVT.isInteger()) ||
5859+
const bool OutOpIsIntOrFP = OpInfo.ConstraintVT.isInteger() ||
5860+
OpInfo.ConstraintVT.isFloatingPoint();
5861+
const bool InOpIsIntOrFP = Input.ConstraintVT.isInteger() ||
5862+
Input.ConstraintVT.isFloatingPoint();
5863+
if ((OutOpIsIntOrFP != InOpIsIntOrFP) ||
58615864
(MatchRC.second != InputRC.second)) {
58625865
report_fatal_error("Unsupported asm: input constraint"
58635866
" 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)