This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Lower inline asm constraints vi, vj and vk.
Changes PlannedPublic

Authored by fakepaper56 on Jul 11 2023, 11:28 PM.

Details

Summary

This validate and lower arguments to inline asm nodes which have the constraints vi, vi and vk, with the following semantics (equivalent to GCC):
vi: vector 5-bit signed immediate.
vj: vector negated 5-bit signed immediate.
vk: vector 5-bit unsigned immediate.

Diff Detail

Event Timeline

fakepaper56 created this revision.Jul 11 2023, 11:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 11 2023, 11:28 PM
fakepaper56 requested review of this revision.Jul 11 2023, 11:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 11 2023, 11:28 PM

Don't we need to update clang too?

craig.topper added inline comments.Jul 12 2023, 12:08 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
16095

Can we merge this if into the previous else so we have

if (Constraint.length() == 1) {
   ...
} else if (Constraint == "vi") {
  ...
} else if (Constraint == "vk") {
 ...
} else if (Constraint == "vj") {
 ...
}

Ooops, hold on, GCC actually is accepting immediate vector value instead of immediate scalar value.

(define_constraint "vi"
  "A vector 5-bit signed immediate."
  (and (match_code "const_vector")
       (match_test "riscv_vector::const_vec_all_same_in_range_p (op, -16, 15)")))

(define_constraint "vj"
  "A vector negated 5-bit signed immediate."
  (and (match_code "const_vector")
       (match_test "riscv_vector::const_vec_all_same_in_range_p (op, -15, 16)")))

(define_constraint "vk"
  "A vector 5-bit unsigned immediate."
  (and (match_code "const_vector")
       (match_test "riscv_vector::const_vec_all_same_in_range_p (op, 0, 31)")))

I mean GCC can't really accept immediate value with either vi, vk or vj:

$ cat x.c
int foo() {
    asm volatile("## %0" :/*write*/ :/*read*/ "vi"(10): /* clobber */);
    return 0;
}

$ riscv64-unknown-linux-gnu-gcc x.c -o - -S -O3
        .file   "x.c"
        .option nopic
        .attribute arch, "rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0"
        .attribute unaligned_access, 0
        .attribute stack_align, 16
        .text
x.c: In function 'foo':
x.c:2:5: warning: 'asm' operand 0 probably does not match constraints
    2 |     asm volatile("## %0" :/*write*/ :/*read*/ "vi"(10): /* clobber */);
      |     ^~~
x.c:2:5: error: impossible constraint in 'asm'
        .align  1
        .globl  foo
        .type   foo, @function
foo:
.LFB0:
        .cfi_startproc
        li      a0,0
        ret
        .cfi_endproc
.LFE0:
        .size   foo, .-foo
fakepaper56 planned changes to this revision.Jul 12 2023, 8:00 PM