Skip to content

Commit

Permalink
[CodeGen][MIR] Support parsing of scalable vectors in MIR (#70893)
Browse files Browse the repository at this point in the history
This patch builds on the support for vectors by adding ability to parse
scalable vectors in MIR and updates error messages to reflect that ability.
  • Loading branch information
michaelmaitland committed Nov 3, 2023
1 parent 65dc96c commit 801a30a
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 9 deletions.
34 changes: 26 additions & 8 deletions llvm/lib/CodeGen/MIRParser/MIParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1946,24 +1946,41 @@ bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {

// Now we're looking for a vector.
if (Token.isNot(MIToken::less))
return error(Loc,
"expected sN, pA, <M x sN>, or <M x pA> for GlobalISel type");
return error(Loc, "expected sN, pA, <M x sN>, <M x pA>, <vscale x M x sN>, "
"or <vscale x M x pA> for GlobalISel type");
lex();

if (Token.isNot(MIToken::IntegerLiteral))
bool HasVScale =
Token.is(MIToken::Identifier) && Token.stringValue() == "vscale";
if (HasVScale) {
lex();
if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
return error("expected <vscale x M x sN> or <vscale x M x pA>");
lex();
}

auto GetError = [this, &HasVScale, Loc]() {
if (HasVScale)
return error(
Loc, "expected <vscale x M x sN> or <vscale M x pA> for vector type");
return error(Loc, "expected <M x sN> or <M x pA> for vector type");
};

if (Token.isNot(MIToken::IntegerLiteral))
return GetError();
uint64_t NumElements = Token.integerValue().getZExtValue();
if (!verifyVectorElementCount(NumElements))
return error("invalid number of vector elements");

lex();

if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
return error(Loc, "expected <M x sN> or <M x pA> for vector type");
return GetError();
lex();

if (Token.range().front() != 's' && Token.range().front() != 'p')
return error(Loc, "expected <M x sN> or <M x pA> for vector type");
return GetError();

StringRef SizeStr = Token.range().drop_front();
if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
return error("expected integers after 's'/'p' type character");
Expand All @@ -1981,14 +1998,15 @@ bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {

Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
} else
return error(Loc, "expected <M x sN> or <M x pA> for vector type");
return GetError();
lex();

if (Token.isNot(MIToken::greater))
return error(Loc, "expected <M x sN> or <M x pA> for vector type");
return GetError();

lex();

Ty = LLT::fixed_vector(NumElements, Ty);
Ty = LLT::vector(ElementCount::get(NumElements, HasVScale), Ty);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ name: test_low_level_type_does_not_start_with_s_p_lt
body: |
bb.0:
liveins: $x0
; CHECK: [[@LINE+1]]:10: expected sN, pA, <M x sN>, or <M x pA> for GlobalISel type
; CHECK: [[@LINE+1]]:10: expected sN, pA, <M x sN>, <M x pA>, <vscale x M x sN>, or <vscale x M x pA> for GlobalISel type
%0:_(i64) = COPY $x0
...
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err0.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s
---
name: err_after_vscale0
body: |
bb.0:
%0:_(<vscale) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale x M x pA>

9 changes: 9 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err1.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s
---
name: err_after_vscale1
body: |
bb.0:
%0:_(<vscale notanx) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale x M x pA>
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err10.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s

---
name: err_after_vscalexMxp
body: |
bb.0:
%0:_(<vscale x 4 x p) = IMPLICIT_DEF
...

# CHECK: expected integers after 's'/'p' type character
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err11.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s

---
name: err_after_vscalexMxs32
body: |
bb.0:
%0:_(<vscale x 4 x s32) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale M x pA> for vector type
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err12.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s

---
name: err_after_vscalexMxp0
body: |
bb.0:
%0:_(<vscale x 4 x p0) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale M x pA> for vector type
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err13.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s

---
name: err_after_vscalexMxs32X
body: |
bb.0:
%0:_(<vscale x 4 x s32 X) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale M x pA> for vector type
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err14.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s

---
name: err_after_vscalexMxp0
body: |
bb.0:
%0:_(<vscale x 4 x p0 X) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale M x pA> for vector type
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err15.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s

---
name: err_after_vscale0
body: |
bb.0:
%0:_(notatype) = IMPLICIT_DEF
...

# CHECK: expected sN, pA, <M x sN>, <M x pA>, <vscale x M x sN>, or <vscale x M x pA> for GlobalISel type
9 changes: 9 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err2.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s
---
name: err_after_vscalex0
body: |
bb.0:
%0:_(<vscale x) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale M x pA> for vector type
9 changes: 9 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err3.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s
---
name: err_after_vscalex0
body: |
bb.0:
%0:_(<vscale x) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale M x pA> for vector type
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err4.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s
---
name: err_after_vscalex1
body: |
bb.0:
%0:_(<vscale x notanint) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale M x pA> for vector type

9 changes: 9 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err5.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s
---
name: err_after_vscalexM
body: |
bb.0:
%0:_(<vscale x 4) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale M x pA> for vector type
11 changes: 11 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err6.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s

---
name: err_after_vscalexMx0
body: |
bb.0:
%0:_(<vscale x 4 x) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale M x pA> for vector type

9 changes: 9 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err7.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s
---
name: err_after_vscalexMx1
body: |
bb.0:
%0:_(<vscale x 4 x notansorp) = IMPLICIT_DEF
...

# CHECK: expected <vscale x M x sN> or <vscale M x pA> for vector type
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err8.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s

---
name: err_after_vscalexMxs
body: |
bb.0:
%0:_(<vscale x 4 x s) = IMPLICIT_DEF
...

# CHECK: expected integers after 's'/'p' type character
11 changes: 11 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type-err9.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# RUN: not llc -run-pass none -o - %s 2>&1 | FileCheck %s
---
name: err_after_vscalexMxpX
body: |
bb.0:
%0:_(<vscale x 4 x pX) = IMPLICIT_DEF
...

# CHECK: expected integers after 's'/'p' type character


133 changes: 133 additions & 0 deletions llvm/test/CodeGen/MIR/Generic/scalable-vector-type.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# RUN: llc -run-pass=none -o - %s | FileCheck %s

---
name: test_nxv1s8
body: |
bb.0:
; CHECK-LABEL: name: test_nxv1s8
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 1 x s8>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 1 x s8>) = COPY [[DEF]](<vscale x 1 x s8>)
%0:_(<vscale x 1 x s8>) = IMPLICIT_DEF
%1:_(<vscale x 1 x s8>) = COPY %0
...

---
name: test_nxv1s16
body: |
bb.0:
; CHECK-LABEL: name: test_nxv1s16
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 1 x s16>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 1 x s16>) = COPY [[DEF]](<vscale x 1 x s16>)
%0:_(<vscale x 1 x s16>) = IMPLICIT_DEF
%1:_(<vscale x 1 x s16>) = COPY %0
...

---
name: test_nxv1s32
body: |
bb.0:
; CHECK-LABEL: name: test_nxv1s32
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 1 x s32>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 1 x s32>) = COPY [[DEF]](<vscale x 1 x s32>)
%0:_(<vscale x 1 x s32>) = IMPLICIT_DEF
%1:_(<vscale x 1 x s32>) = COPY %0
...

---
name: test_nxv1s64
body: |
bb.0:
; CHECK-LABEL: name: test_nxv1s64
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 1 x s64>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 1 x s64>) = COPY [[DEF]](<vscale x 1 x s64>)
%0:_(<vscale x 1 x s64>) = IMPLICIT_DEF
%1:_(<vscale x 1 x s64>) = COPY %0
...

---
name: test_nxv4s8
body: |
bb.0:
; CHECK-LABEL: name: test_nxv4s8
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 4 x s8>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 4 x s8>) = COPY [[DEF]](<vscale x 4 x s8>)
%0:_(<vscale x 4 x s8>) = IMPLICIT_DEF
%1:_(<vscale x 4 x s8>) = COPY %0
...

---
name: test_nxv4s16
body: |
bb.0:
; CHECK-LABEL: name: test_nxv4s16
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 4 x s16>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 4 x s16>) = COPY [[DEF]](<vscale x 4 x s16>)
%0:_(<vscale x 4 x s16>) = IMPLICIT_DEF
%1:_(<vscale x 4 x s16>) = COPY %0
...

---
name: test_nxv4s32
body: |
bb.0:
; CHECK-LABEL: name: test_nxv4s32
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 4 x s32>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 4 x s32>) = COPY [[DEF]](<vscale x 4 x s32>)
%0:_(<vscale x 4 x s32>) = IMPLICIT_DEF
%1:_(<vscale x 4 x s32>) = COPY %0
...

---
name: test_nxv4s64
body: |
bb.0:
; CHECK-LABEL: name: test_nxv4s64
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 4 x s64>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 4 x s64>) = COPY [[DEF]](<vscale x 4 x s64>)
%0:_(<vscale x 4 x s64>) = IMPLICIT_DEF
%1:_(<vscale x 4 x s64>) = COPY %0
...

---
name: test_nxv1p0
body: |
bb.0:
; CHECK-LABEL: name: test_nxv1p0
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 1 x p0>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 1 x p0>) = COPY [[DEF]](<vscale x 1 x p0>)
%0:_(<vscale x 1 x p0>) = IMPLICIT_DEF
%1:_(<vscale x 1 x p0>) = COPY %0
...

---
name: test_nxv1sp1
body: |
bb.0:
; CHECK-LABEL: name: test_nxv1sp1
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 1 x p0>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 1 x p0>) = COPY [[DEF]](<vscale x 1 x p0>)
%0:_(<vscale x 1 x p0>) = IMPLICIT_DEF
%1:_(<vscale x 1 x p0>) = COPY %0
...

---
name: test_nxv4p0
body: |
bb.0:
; CHECK-LABEL: name: test_nxv4p0
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 4 x p0>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 4 x p0>) = COPY [[DEF]](<vscale x 4 x p0>)
%0:_(<vscale x 4 x p0>) = IMPLICIT_DEF
%1:_(<vscale x 4 x p0>) = COPY %0
...

---
name: test_nxv4p1
body: |
bb.0:
; CHECK-LABEL: name: test_nxv4p1
; CHECK: [[DEF:%[0-9]+]]:_(<vscale x 4 x p1>) = IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<vscale x 4 x p1>) = COPY [[DEF]](<vscale x 4 x p1>)
%0:_(<vscale x 4 x p1>) = IMPLICIT_DEF
%1:_(<vscale x 4 x p1>) = COPY %0
...

0 comments on commit 801a30a

Please sign in to comment.