Skip to content

[WebAssembly,MC] Add ref.test_func handling to AsmParser#139642

Merged
sbc100 merged 4 commits into
llvm:mainfrom
hoodmane:add-ref-test-func
Jul 7, 2025
Merged

[WebAssembly,MC] Add ref.test_func handling to AsmParser#139642
sbc100 merged 4 commits into
llvm:mainfrom
hoodmane:add-ref-test-func

Conversation

@hoodmane

Copy link
Copy Markdown
Contributor

This will allow us to eliminate the hand-coded webassembly here:
https://github.com/python/cpython/blob/main/Python/emscripten_trampoline.c#L138

cc @tlively @sbc100

@llvmbot llvmbot added backend:WebAssembly llvm:mc Machine (object) code labels May 12, 2025
@llvmbot

llvmbot commented May 12, 2025

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-mc

Author: Hood Chatham (hoodmane)

Changes

This will allow us to eliminate the hand-coded webassembly here:
https://github.com/python/cpython/blob/main/Python/emscripten_trampoline.c#L138

cc @tlively @sbc100


Full diff: https://github.com/llvm/llvm-project/pull/139642.diff

3 Files Affected:

  • (modified) llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp (+2)
  • (modified) llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td (+8)
  • (modified) llvm/test/MC/WebAssembly/reference-types.s (+15)
diff --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
index 7ee6a3d8304be..3fad1710c30c4 100644
--- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
+++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
@@ -668,6 +668,8 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
       if (parseFunctionTableOperand(&FunctionTable))
         return true;
       ExpectFuncType = true;
+    } else if (Name == "ref.test_func") {
+      ExpectFuncType = true;
     }
 
     // Returns true if the next tokens are a catch clause
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td b/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
index 2654a09387fd4..8786d78c8e5af 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
@@ -36,6 +36,14 @@ multiclass REF_I<WebAssemblyRegClass rc, ValueType vt, string ht> {
         Requires<[HasReferenceTypes]>;
 }
 
+defm REF_TEST_FUNCREF :
+  I<(outs I32: $res),
+    (ins TypeIndex:$type, FUNCREF: $ref),
+    (outs),
+    (ins TypeIndex:$type),
+    [],
+    "ref.test_func\t$type, $ref", "ref.test_func $type", 0xfb14>;
+
 defm "" : REF_I<FUNCREF, funcref, "func">;
 defm "" : REF_I<EXTERNREF, externref, "extern">;
 defm "" : REF_I<EXNREF, exnref, "exn">;
diff --git a/llvm/test/MC/WebAssembly/reference-types.s b/llvm/test/MC/WebAssembly/reference-types.s
index cfadede8295ef..8f3bca79bb68f 100644
--- a/llvm/test/MC/WebAssembly/reference-types.s
+++ b/llvm/test/MC/WebAssembly/reference-types.s
@@ -27,6 +27,21 @@ ref_null_test:
   drop
   end_function
 
+# CHECK-LABEL: ref_test_test:
+# CHECK: ref.null_func   # encoding: [0xd0,0x70]
+# CHECK: ref.test () -> () # encoding: [0xfb,0x14,0x80'A',0x80'A',0x80'A',0x80'A',A]
+# CHECK: # fixup A - offset: 2, value: .Ltypeindex0@TYPEINDEX, kind: fixup_uleb128_i32
+# CHECK: ref.null_func   # encoding: [0xd0,0x70]
+# CHECK: ref.test () -> (i32) # encoding: [0xfb,0x14,0x80'A',0x80'A',0x80'A',0x80'A',A]
+# CHECK: # fixup A - offset: 2, value: .Ltypeindex1@TYPEINDEX, kind: fixup_uleb128_i32
+ref_test_test:
+  .functype ref_test_test () -> (i32, i32)
+  ref.null_func
+  ref.test_func () -> ()
+  ref.null_func
+  ref.test_func () -> (i32)
+  end_function
+
 # CHECK-LABEL: ref_sig_test_funcref:
 # CHECK-NEXT: .functype ref_sig_test_funcref (funcref) -> (funcref)
 ref_sig_test_funcref:

@llvmbot

llvmbot commented May 12, 2025

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-backend-webassembly

Author: Hood Chatham (hoodmane)

Changes

This will allow us to eliminate the hand-coded webassembly here:
https://github.com/python/cpython/blob/main/Python/emscripten_trampoline.c#L138

cc @tlively @sbc100


Full diff: https://github.com/llvm/llvm-project/pull/139642.diff

3 Files Affected:

  • (modified) llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp (+2)
  • (modified) llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td (+8)
  • (modified) llvm/test/MC/WebAssembly/reference-types.s (+15)
diff --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
index 7ee6a3d8304be..3fad1710c30c4 100644
--- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
+++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
@@ -668,6 +668,8 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
       if (parseFunctionTableOperand(&FunctionTable))
         return true;
       ExpectFuncType = true;
+    } else if (Name == "ref.test_func") {
+      ExpectFuncType = true;
     }
 
     // Returns true if the next tokens are a catch clause
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td b/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
index 2654a09387fd4..8786d78c8e5af 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
@@ -36,6 +36,14 @@ multiclass REF_I<WebAssemblyRegClass rc, ValueType vt, string ht> {
         Requires<[HasReferenceTypes]>;
 }
 
+defm REF_TEST_FUNCREF :
+  I<(outs I32: $res),
+    (ins TypeIndex:$type, FUNCREF: $ref),
+    (outs),
+    (ins TypeIndex:$type),
+    [],
+    "ref.test_func\t$type, $ref", "ref.test_func $type", 0xfb14>;
+
 defm "" : REF_I<FUNCREF, funcref, "func">;
 defm "" : REF_I<EXTERNREF, externref, "extern">;
 defm "" : REF_I<EXNREF, exnref, "exn">;
diff --git a/llvm/test/MC/WebAssembly/reference-types.s b/llvm/test/MC/WebAssembly/reference-types.s
index cfadede8295ef..8f3bca79bb68f 100644
--- a/llvm/test/MC/WebAssembly/reference-types.s
+++ b/llvm/test/MC/WebAssembly/reference-types.s
@@ -27,6 +27,21 @@ ref_null_test:
   drop
   end_function
 
+# CHECK-LABEL: ref_test_test:
+# CHECK: ref.null_func   # encoding: [0xd0,0x70]
+# CHECK: ref.test () -> () # encoding: [0xfb,0x14,0x80'A',0x80'A',0x80'A',0x80'A',A]
+# CHECK: # fixup A - offset: 2, value: .Ltypeindex0@TYPEINDEX, kind: fixup_uleb128_i32
+# CHECK: ref.null_func   # encoding: [0xd0,0x70]
+# CHECK: ref.test () -> (i32) # encoding: [0xfb,0x14,0x80'A',0x80'A',0x80'A',0x80'A',A]
+# CHECK: # fixup A - offset: 2, value: .Ltypeindex1@TYPEINDEX, kind: fixup_uleb128_i32
+ref_test_test:
+  .functype ref_test_test () -> (i32, i32)
+  ref.null_func
+  ref.test_func () -> ()
+  ref.null_func
+  ref.test_func () -> (i32)
+  end_function
+
 # CHECK-LABEL: ref_sig_test_funcref:
 # CHECK-NEXT: .functype ref_sig_test_funcref (funcref) -> (funcref)
 ref_sig_test_funcref:

Comment thread llvm/test/MC/WebAssembly/reference-types.s
return true;
ExpectFuncType = true;
} else if (Name == "ref.test") {
ExpectFuncType = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the long run I guess this should instead be something like ExpectRefType?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah exactly. But there is no such thing yet...

@hoodmane hoodmane Jul 7, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment/

@github-actions

github-actions Bot commented Jul 7, 2025

Copy link
Copy Markdown

✅ With the latest revision this PR passed the C/C++ code formatter.

@hoodmane hoodmane changed the title [WebAssembly] Add ref.test_func handling to AsmParser [WebAssembly,MC] Add ref.test_func handling to AsmParser Jul 7, 2025
@hoodmane

hoodmane commented Jul 7, 2025

Copy link
Copy Markdown
Contributor Author

@sbc100 could you merge it?

@sbc100 sbc100 merged commit 42d079e into llvm:main Jul 7, 2025
9 checks passed
@hoodmane hoodmane deleted the add-ref-test-func branch July 7, 2025 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:WebAssembly llvm:mc Machine (object) code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants