[llvm] [WebAssembly] Support MIR printing of reference types (PR #113028)

Heejin Ahn via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 19 11:12:33 PDT 2024


https://github.com/aheejin updated https://github.com/llvm/llvm-project/pull/113028

>From e5eb3131814520da39bb67f52252d40c6acb28b1 Mon Sep 17 00:00:00 2001
From: Heejin Ahn <aheejin at gmail.com>
Date: Sat, 19 Oct 2024 05:20:07 +0000
Subject: [PATCH] [WebAssembly] Support MIR printing of reference types

When printing a memory operand in MIR, this line
https://github.com/llvm/llvm-project/blob/d37bc32a65651e647148236ffb9728ea2e77eac3/llvm/lib/CodeGen/MachineOperand.cpp#L1247
calls this
https://github.com/llvm/llvm-project/blob/d37bc32a65651e647148236ffb9728ea2e77eac3/llvm/include/llvm/Support/Alignment.h#L238
which assumes `Rhs` (the size in this case) is positive.

But Wasm reference types' size is set to 0:
https://github.com/llvm/llvm-project/blob/d37bc32a65651e647148236ffb9728ea2e77eac3/llvm/include/llvm/CodeGen/ValueTypes.td#L326-L328

It looks they didn't have a problem being printed in MIR until #84751,
which somehow removed the condition of `getSize() > 0` in that line.
This revives the condition so that Wasm reference types will not crash
the MIR printer.
---
 llvm/lib/CodeGen/MachineOperand.cpp                  | 3 ++-
 llvm/test/CodeGen/WebAssembly/externref-globalget.ll | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/MachineOperand.cpp b/llvm/lib/CodeGen/MachineOperand.cpp
index 89d32c3f005e00..c0e004555de959 100644
--- a/llvm/lib/CodeGen/MachineOperand.cpp
+++ b/llvm/lib/CodeGen/MachineOperand.cpp
@@ -1244,7 +1244,8 @@ void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
   }
   MachineOperand::printOperandOffset(OS, getOffset());
   if (!getSize().hasValue() ||
-      getAlign() != getSize().getValue().getKnownMinValue())
+      (!getSize().isZero() &&
+       getAlign() != getSize().getValue().getKnownMinValue()))
     OS << ", align " << getAlign().value();
   if (getAlign() != getBaseAlign())
     OS << ", basealign " << getBaseAlign().value();
diff --git a/llvm/test/CodeGen/WebAssembly/externref-globalget.ll b/llvm/test/CodeGen/WebAssembly/externref-globalget.ll
index cdf98c42439f2d..79d7932486e226 100644
--- a/llvm/test/CodeGen/WebAssembly/externref-globalget.ll
+++ b/llvm/test/CodeGen/WebAssembly/externref-globalget.ll
@@ -1,4 +1,7 @@
 ; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false -mattr=+reference-types | FileCheck %s
+; Test for MIR printing of reference types in other address spaces. This should
+; not error out.
+; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false -mattr=+reference-types -print-after=finalize-isel | FileCheck %s
 
 %externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
 



More information about the llvm-commits mailing list