https://github.com/lnihlen created 
https://github.com/llvm/llvm-project/pull/93428

This is a re-application of #71540, with the hopes I can get some help from 
@pmatos and others to finish it. Right now the 
'../clang/test/CodeGen/WebAssembly/wasm-funcref.c' test fails with an assertion 
trying to cast the target("wasm.funcref") instruction to a pointer. Looking in 
to that further but wanted to checkpoint the code here from the first patch.

>From 2f38d84d921bd3c4fe00ed6bfd360943d8cda15f Mon Sep 17 00:00:00 2001
From: Lucile Nihlen <luci.the.r...@gmail.com>
Date: Sat, 25 May 2024 18:55:09 +0000
Subject: [PATCH 1/2] Apply patch from #71540

---
 .../test/CodeGen/WebAssembly/builtins-table.c | 16 ++--
 .../test/CodeGen/WebAssembly/wasm-externref.c |  9 +-
 clang/test/CodeGen/builtins-wasm.c            |  2 +-
 llvm/include/llvm/IR/Intrinsics.h             |  2 +
 llvm/include/llvm/IR/IntrinsicsWebAssembly.td |  7 ++
 llvm/lib/CodeGen/ValueTypes.cpp               |  4 +
 llvm/lib/IR/Function.cpp                      | 14 ++-
 llvm/lib/IR/Type.cpp                          | 10 ++-
 llvm/lib/Target/WebAssembly/CMakeLists.txt    |  1 -
 .../WebAssembly/Utils/WasmAddressSpaces.h     |  4 -
 .../Utils/WebAssemblyTypeUtilities.h          | 10 +--
 llvm/lib/Target/WebAssembly/WebAssembly.h     |  2 -
 .../lib/Target/WebAssembly/WebAssemblyISD.def |  3 +
 .../WebAssembly/WebAssemblyISelLowering.cpp   | 22 +----
 .../WebAssembly/WebAssemblyISelLowering.h     |  3 -
 .../Target/WebAssembly/WebAssemblyInstrRef.td |  8 ++
 .../WebAssemblyLowerRefTypesIntPtrConv.cpp    | 86 -------------------
 .../WebAssembly/WebAssemblyTargetMachine.cpp  |  2 -
 .../WebAssembly/externref-globalget.ll        |  2 +-
 .../WebAssembly/externref-globalset.ll        |  2 +-
 .../CodeGen/WebAssembly/externref-inttoptr.ll | 12 +--
 .../CodeGen/WebAssembly/externref-ptrtoint.ll |  2 +-
 .../CodeGen/WebAssembly/externref-tableget.ll |  2 +-
 .../CodeGen/WebAssembly/externref-tableset.ll |  2 +-
 .../WebAssembly/externref-unsized-load.ll     |  4 +-
 .../WebAssembly/externref-unsized-store.ll    |  4 +-
 llvm/test/CodeGen/WebAssembly/funcref-call.ll | 10 ++-
 .../CodeGen/WebAssembly/funcref-globalget.ll  |  2 +-
 .../CodeGen/WebAssembly/funcref-globalset.ll  |  2 +-
 .../CodeGen/WebAssembly/funcref-table_call.ll |  2 +-
 .../CodeGen/WebAssembly/funcref-tableget.ll   |  2 +-
 .../CodeGen/WebAssembly/funcref-tableset.ll   |  2 +-
 llvm/test/CodeGen/WebAssembly/ref-null.ll     |  4 +-
 llvm/test/CodeGen/WebAssembly/table-copy.ll   |  2 +-
 llvm/test/CodeGen/WebAssembly/table-fill.ll   |  2 +-
 llvm/test/CodeGen/WebAssembly/table-grow.ll   |  2 +-
 llvm/test/CodeGen/WebAssembly/table-size.ll   |  2 +-
 llvm/test/CodeGen/WebAssembly/table-types.ll  |  4 +-
 .../llvm/lib/Target/WebAssembly/BUILD.gn      |  1 -
 39 files changed, 93 insertions(+), 179 deletions(-)
 delete mode 100644 
llvm/lib/Target/WebAssembly/WebAssemblyLowerRefTypesIntPtrConv.cpp

diff --git a/clang/test/CodeGen/WebAssembly/builtins-table.c 
b/clang/test/CodeGen/WebAssembly/builtins-table.c
index 74bb2442fe552..eeed335855e40 100644
--- a/clang/test/CodeGen/WebAssembly/builtins-table.c
+++ b/clang/test/CodeGen/WebAssembly/builtins-table.c
@@ -7,17 +7,17 @@ static __externref_t table[0];
 // CHECK-LABEL: define {{[^@]+}}@test_builtin_wasm_table_get
 // CHECK-SAME: (i32 noundef [[INDEX:%.*]]) #[[ATTR0:[0-9]+]] {
 // CHECK-NEXT:  entry:
-// CHECK-NEXT:    [[TMP0:%.*]] = call ptr addrspace(10) 
@llvm.wasm.table.get.externref(ptr addrspace(1) @table, i32 [[INDEX]])
-// CHECK-NEXT:    ret ptr addrspace(10) [[TMP0]]
+// CHECK-NEXT:    [[TMP0:%.*]] = call target("wasm.externref") 
@llvm.wasm.table.get.externref(ptr addrspace(1) @table, i32 [[INDEX]])
+// CHECK-NEXT:    ret target("wasm.externref") [[TMP0]]
 //
 __externref_t test_builtin_wasm_table_get(int index) {
   return __builtin_wasm_table_get(table, index);
 }
 
 // CHECK-LABEL: define {{[^@]+}}@test_builtin_wasm_table_set
-// CHECK-SAME: (i32 noundef [[INDEX:%.*]], ptr addrspace(10) [[REF:%.*]]) 
#[[ATTR0]] {
+// CHECK-SAME: (i32 noundef [[INDEX:%.*]], target("wasm.externref") 
[[REF:%.*]]) #[[ATTR0]] {
 // CHECK-NEXT:  entry:
-// CHECK-NEXT:    call void @llvm.wasm.table.set.externref(ptr addrspace(1) 
@table, i32 [[INDEX]], ptr addrspace(10) [[REF]])
+// CHECK-NEXT:    call void @llvm.wasm.table.set.externref(ptr addrspace(1) 
@table, i32 [[INDEX]], target("wasm.externref") [[REF]])
 // CHECK-NEXT:    ret void
 //
 void test_builtin_wasm_table_set(int index, __externref_t ref) {
@@ -35,9 +35,9 @@ int test_builtin_wasm_table_size() {
 }
 
 // CHECK-LABEL: define {{[^@]+}}@test_builtin_wasm_table_grow
-// CHECK-SAME: (ptr addrspace(10) [[REF:%.*]], i32 noundef [[NELEM:%.*]]) 
#[[ATTR0]] {
+// CHECK-SAME: (target("wasm.externref") [[REF:%.*]], i32 noundef 
[[NELEM:%.*]]) #[[ATTR0]] {
 // CHECK-NEXT:  entry:
-// CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.wasm.table.grow.externref(ptr 
addrspace(1) @table, ptr addrspace(10) [[REF]], i32 [[NELEM]])
+// CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.wasm.table.grow.externref(ptr 
addrspace(1) @table, target("wasm.externref") [[REF]], i32 [[NELEM]])
 // CHECK-NEXT:    ret i32 [[TMP0]]
 //
 int test_builtin_wasm_table_grow(__externref_t ref, int nelem) {
@@ -45,9 +45,9 @@ int test_builtin_wasm_table_grow(__externref_t ref, int 
nelem) {
 }
 
 // CHECK-LABEL: define {{[^@]+}}@test_builtin_wasm_table_fill
-// CHECK-SAME: (i32 noundef [[INDEX:%.*]], ptr addrspace(10) [[REF:%.*]], i32 
noundef [[NELEM:%.*]]) #[[ATTR0]] {
+// CHECK-SAME: (i32 noundef [[INDEX:%.*]], target("wasm.externref") 
[[REF:%.*]], i32 noundef [[NELEM:%.*]]) #[[ATTR0]] {
 // CHECK-NEXT:  entry:
-// CHECK-NEXT:    call void @llvm.wasm.table.fill.externref(ptr addrspace(1) 
@table, i32 [[INDEX]], ptr addrspace(10) [[REF]], i32 [[NELEM]])
+// CHECK-NEXT:    call void @llvm.wasm.table.fill.externref(ptr addrspace(1) 
@table, i32 [[INDEX]], target("wasm.externref") [[REF]], i32 [[NELEM]])
 // CHECK-NEXT:    ret void
 //
 void test_builtin_wasm_table_fill(int index, __externref_t ref, int nelem) {
diff --git a/clang/test/CodeGen/WebAssembly/wasm-externref.c 
b/clang/test/CodeGen/WebAssembly/wasm-externref.c
index 788438bb4a86a..38f3d7289c8d4 100644
--- a/clang/test/CodeGen/WebAssembly/wasm-externref.c
+++ b/clang/test/CodeGen/WebAssembly/wasm-externref.c
@@ -7,12 +7,13 @@ void helper(externref_t);
 
 // CHECK-LABEL: @handle(
 // CHECK-NEXT:  entry:
-// CHECK-NEXT:    [[OBJ_ADDR:%.*]] = alloca ptr addrspace(10), align 1
-// CHECK-NEXT:    store ptr addrspace(10) [[OBJ:%.*]], ptr [[OBJ_ADDR]], align 
1
-// CHECK-NEXT:    [[TMP0:%.*]] = load ptr addrspace(10), ptr [[OBJ_ADDR]], 
align 1
-// CHECK-NEXT:    call void @helper(ptr addrspace(10) [[TMP0]])
+// CHECK-NEXT:    [[OBJ_ADDR:%.*]] = alloca target("wasm.externref"), align 1
+// CHECK-NEXT:    store target("wasm.externref") [[OBJ:%.*]], ptr 
[[OBJ_ADDR]], align 1
+// CHECK-NEXT:    [[TMP0:%.*]] = load target("wasm.externref"), ptr 
[[OBJ_ADDR]], align 1
+// CHECK-NEXT:    call void @helper(target("wasm.externref") [[TMP0]])
 // CHECK-NEXT:    ret void
 //
 void handle(externref_t obj) {
   helper(obj);
 }
+
diff --git a/clang/test/CodeGen/builtins-wasm.c 
b/clang/test/CodeGen/builtins-wasm.c
index 93a6ab06081c9..4c6fc1e6b6bb3 100644
--- a/clang/test/CodeGen/builtins-wasm.c
+++ b/clang/test/CodeGen/builtins-wasm.c
@@ -827,6 +827,6 @@ float extract_lane_f16x8(f16x8 a, int i) {
 }
 __externref_t externref_null() {
   return __builtin_wasm_ref_null_extern();
-  // WEBASSEMBLY: tail call ptr addrspace(10) @llvm.wasm.ref.null.extern()
+  // WEBASSEMBLY: tail call target("wasm.externref") 
@llvm.wasm.ref.null.extern()
   // WEBASSEMBLY-NEXT: ret
 }
diff --git a/llvm/include/llvm/IR/Intrinsics.h 
b/llvm/include/llvm/IR/Intrinsics.h
index f79df522dc805..88b28eb362087 100644
--- a/llvm/include/llvm/IR/Intrinsics.h
+++ b/llvm/include/llvm/IR/Intrinsics.h
@@ -144,6 +144,8 @@ namespace Intrinsic {
       AMX,
       PPCQuad,
       AArch64Svcount,
+      WasmExternref,
+      WasmFuncref,
     } Kind;
 
     union {
diff --git a/llvm/include/llvm/IR/IntrinsicsWebAssembly.td 
b/llvm/include/llvm/IR/IntrinsicsWebAssembly.td
index 237f268784bb0..7384a0d8c726e 100644
--- a/llvm/include/llvm/IR/IntrinsicsWebAssembly.td
+++ b/llvm/include/llvm/IR/IntrinsicsWebAssembly.td
@@ -38,6 +38,13 @@ def int_wasm_ref_is_null_func :
   DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_funcref_ty],
                         [IntrNoMem], "llvm.wasm.ref.is_null.func">;
 
+//===----------------------------------------------------------------------===//
+// funcref target ext type -> pointer intrinsic
+//===----------------------------------------------------------------------===//
+def int_wasm_funcref_to_ptr:
+  DefaultAttrsIntrinsic<[llvm_ptr_ty], [llvm_funcref_ty],
+                        [IntrNoMem], "llvm.wasm.funcref.to_ptr">;
+
 
//===----------------------------------------------------------------------===//
 // Table intrinsics
 
//===----------------------------------------------------------------------===//
diff --git a/llvm/lib/CodeGen/ValueTypes.cpp b/llvm/lib/CodeGen/ValueTypes.cpp
index 3d5c58d282da5..c164bb56313ea 100644
--- a/llvm/lib/CodeGen/ValueTypes.cpp
+++ b/llvm/lib/CodeGen/ValueTypes.cpp
@@ -604,6 +604,10 @@ MVT MVT::getVT(Type *Ty, bool HandleUnknown){
     TargetExtType *TargetExtTy = cast<TargetExtType>(Ty);
     if (TargetExtTy->getName() == "aarch64.svcount")
       return MVT(MVT::aarch64svcount);
+    else if (TargetExtTy->getName() == "wasm.externref")
+      return MVT(MVT::externref);
+    else if (TargetExtTy->getName() = "wasm.funcref")
+      return MVT(MVT::funcref);
     else if (TargetExtTy->getName().starts_with("spirv."))
       return MVT(MVT::spirvbuiltin);
     if (HandleUnknown)
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index bd06ff82a15a5..40964c7a33eaa 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -1209,10 +1209,10 @@ static void DecodeIITType(unsigned &NextElt, 
ArrayRef<unsigned char> Infos,
     DecodeIITType(NextElt, Infos, Info, OutputTable);
     return;
   case IIT_EXTERNREF:
-    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 10));
+    OutputTable.push_back(IITDescriptor::get(IITDescriptor::WasmExternref, 0));
     return;
   case IIT_FUNCREF:
-    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 20));
+    OutputTable.push_back(IITDescriptor::get(IITDescriptor::WasmFuncref, 0));
     return;
   case IIT_PTR:
     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
@@ -1365,6 +1365,10 @@ static Type 
*DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
   case IITDescriptor::PPCQuad: return Type::getPPC_FP128Ty(Context);
   case IITDescriptor::AArch64Svcount:
     return TargetExtType::get(Context, "aarch64.svcount");
+  case IITDescriptor::WasmExternref:
+    return TargetExtType::get(Context, "wasm.externref");
+  case IITDescriptor::WasmFuncref:
+    return TargetExtType::get(Context, "wasm.funcref");
 
   case IITDescriptor::Integer:
     return IntegerType::get(Context, D.Integer_Width);
@@ -1550,6 +1554,12 @@ static bool matchIntrinsicType(
     case IITDescriptor::AArch64Svcount:
       return !isa<TargetExtType>(Ty) ||
              cast<TargetExtType>(Ty)->getName() != "aarch64.svcount";
+    case IITDescriptor::WasmExternref:
+      return !isa<TargetExtType>(Ty) ||
+            cast<TargetExtType>(Ty)->getName() != "wasm.externref";
+    case IITDescriptor::WasmFuncref:
+      return !isa<TargetExtType>(Ty) ||
+            cast<TargetExtType>(Ty)->getName() != "wasm.funcref";
     case IITDescriptor::Vector: {
       VectorType *VT = dyn_cast<VectorType>(Ty);
       return !VT || VT->getElementCount() != D.Vector_Width ||
diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp
index 5c61ad9f000b0..6d0964cbfecb0 100644
--- a/llvm/lib/IR/Type.cpp
+++ b/llvm/lib/IR/Type.cpp
@@ -260,14 +260,12 @@ IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
 }
 
 Type *Type::getWasm_ExternrefTy(LLVMContext &C) {
-  // opaque pointer in addrspace(10)
-  static PointerType *Ty = PointerType::get(C, 10);
+  static TargetExtType *Ty = TargetExtType::get(C, "wasm.externref", {}, {});
   return Ty;
 }
 
 Type *Type::getWasm_FuncrefTy(LLVMContext &C) {
-  // opaque pointer in addrspace(20)
-  static PointerType *Ty = PointerType::get(C, 20);
+  static TargetExtType *Ty = TargetExtType::get(C, "wasm.funcref", {}, {});
   return Ty;
 }
 
@@ -845,6 +843,10 @@ static TargetTypeInfo getTargetTypeInfo(const 
TargetExtType *Ty) {
     return TargetTypeInfo(ScalableVectorType::get(Type::getInt1Ty(C), 16),
                           TargetExtType::HasZeroInit);
 
+  // Opaque types in the WebAssembly name space.
+  if (Name.startswith("wasm."))
+    return TargetTypeInfo(PointerType::getUnqual(C), 
TargetExtType::HasZeroInit, TargetExtType::CanBeGlobal);
+
   return TargetTypeInfo(Type::getVoidTy(C));
 }
 
diff --git a/llvm/lib/Target/WebAssembly/CMakeLists.txt 
b/llvm/lib/Target/WebAssembly/CMakeLists.txt
index 1e83cbeac50d6..b5f9aeb8aa0ba 100644
--- a/llvm/lib/Target/WebAssembly/CMakeLists.txt
+++ b/llvm/lib/Target/WebAssembly/CMakeLists.txt
@@ -36,7 +36,6 @@ add_llvm_target(WebAssemblyCodeGen
   WebAssemblyInstrInfo.cpp
   WebAssemblyLowerBrUnless.cpp
   WebAssemblyLowerEmscriptenEHSjLj.cpp
-  WebAssemblyLowerRefTypesIntPtrConv.cpp
   WebAssemblyMachineFunctionInfo.cpp
   WebAssemblyMCInstLower.cpp
   WebAssemblyMCLowerPrePass.cpp
diff --git a/llvm/lib/Target/WebAssembly/Utils/WasmAddressSpaces.h 
b/llvm/lib/Target/WebAssembly/Utils/WasmAddressSpaces.h
index 2239badca69c3..72853d479ed26 100644
--- a/llvm/lib/Target/WebAssembly/Utils/WasmAddressSpaces.h
+++ b/llvm/lib/Target/WebAssembly/Utils/WasmAddressSpaces.h
@@ -25,10 +25,6 @@ enum WasmAddressSpace : unsigned {
   // to these pointers are lowered to global.get / global.set or local.get /
   // local.set, as appropriate.
   WASM_ADDRESS_SPACE_VAR = 1,
-  // A non-integral address space for externref values
-  WASM_ADDRESS_SPACE_EXTERNREF = 10,
-  // A non-integral address space for funcref values
-  WASM_ADDRESS_SPACE_FUNCREF = 20,
 };
 
 inline bool isDefaultAddressSpace(unsigned AS) {
diff --git a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h 
b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
index 87660947e7de1..8e7ef92e8c677 100644
--- a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
+++ b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
@@ -28,16 +28,14 @@ namespace WebAssembly {
 
 /// Return true if this is a WebAssembly Externref Type.
 inline bool isWebAssemblyExternrefType(const Type *Ty) {
-  return Ty->isPointerTy() &&
-         Ty->getPointerAddressSpace() ==
-             WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF;
+  const TargetExtType *TargetTy = dyn_cast<TargetExtType>(Ty);
+  return TargetTy && TargetTy->getName() == "wasm.externref";
 }
 
 /// Return true if this is a WebAssembly Funcref Type.
 inline bool isWebAssemblyFuncrefType(const Type *Ty) {
-  return Ty->isPointerTy() &&
-         Ty->getPointerAddressSpace() ==
-             WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF;
+  const TargetExtType *TargetTy = dyn_cast<TargetExtType>(Ty);
+  return TargetTy && TargetTy->getName() == "wasm.funcref";
 }
 
 /// Return true if this is a WebAssembly Reference Type.
diff --git a/llvm/lib/Target/WebAssembly/WebAssembly.h 
b/llvm/lib/Target/WebAssembly/WebAssembly.h
index 7fc8546248f16..6c84bc979ae4e 100644
--- a/llvm/lib/Target/WebAssembly/WebAssembly.h
+++ b/llvm/lib/Target/WebAssembly/WebAssembly.h
@@ -29,7 +29,6 @@ ModulePass *createWebAssemblyLowerEmscriptenEHSjLj();
 ModulePass *createWebAssemblyAddMissingPrototypes();
 ModulePass *createWebAssemblyFixFunctionBitcasts();
 FunctionPass *createWebAssemblyOptimizeReturned();
-FunctionPass *createWebAssemblyLowerRefTypesIntPtrConv();
 FunctionPass *createWebAssemblyRefTypeMem2Local();
 
 // ISel and immediate followup passes.
@@ -76,7 +75,6 @@ void 
initializeWebAssemblyFixIrreducibleControlFlowPass(PassRegistry &);
 void initializeWebAssemblyLateEHPreparePass(PassRegistry &);
 void initializeWebAssemblyLowerBrUnlessPass(PassRegistry &);
 void initializeWebAssemblyLowerEmscriptenEHSjLjPass(PassRegistry &);
-void initializeWebAssemblyLowerRefTypesIntPtrConvPass(PassRegistry &);
 void initializeWebAssemblyMCLowerPrePassPass(PassRegistry &);
 void initializeWebAssemblyMemIntrinsicResultsPass(PassRegistry &);
 void initializeWebAssemblyNullifyDebugValueListsPass(PassRegistry &);
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISD.def 
b/llvm/lib/Target/WebAssembly/WebAssemblyISD.def
index b8954f4693f0a..fb37b79781715 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISD.def
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISD.def
@@ -45,6 +45,9 @@ HANDLE_NODETYPE(DEMOTE_ZERO)
 HANDLE_NODETYPE(MEMORY_COPY)
 HANDLE_NODETYPE(MEMORY_FILL)
 
+// Pointer conversion intrinsic for funcref
+HANDLE_NODETYPE(FUNCREF_TO_PTR)
+
 // Memory intrinsics
 HANDLE_MEM_NODETYPE(GLOBAL_GET)
 HANDLE_MEM_NODETYPE(GLOBAL_SET)
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp 
b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
index 518b6932a0c87..b640467cbc6f2 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
@@ -365,24 +365,6 @@ WebAssemblyTargetLowering::WebAssemblyTargetLowering(
   setMinimumJumpTableEntries(2);
 }
 
-MVT WebAssemblyTargetLowering::getPointerTy(const DataLayout &DL,
-                                            uint32_t AS) const {
-  if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF)
-    return MVT::externref;
-  if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF)
-    return MVT::funcref;
-  return TargetLowering::getPointerTy(DL, AS);
-}
-
-MVT WebAssemblyTargetLowering::getPointerMemTy(const DataLayout &DL,
-                                               uint32_t AS) const {
-  if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF)
-    return MVT::externref;
-  if (AS == WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF)
-    return MVT::funcref;
-  return TargetLowering::getPointerMemTy(DL, AS);
-}
-
 TargetLowering::AtomicExpansionKind
 WebAssemblyTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
   // We have wasm instructions for these
@@ -1262,9 +1244,7 @@ WebAssemblyTargetLowering::LowerCall(CallLoweringInfo 
&CLI,
     SDValue TableSet = DAG.getMemIntrinsicNode(
         WebAssemblyISD::TABLE_SET, DL, DAG.getVTList(MVT::Other), TableSetOps,
         MVT::funcref,
-        // Machine Mem Operand args
-        MachinePointerInfo(
-            WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF),
+        MachinePointerInfo(),
         CLI.CB->getCalledOperand()->getPointerAlignment(DAG.getDataLayout()),
         MachineMemOperand::MOStore);
 
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h 
b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
index 1d1338ab40d0e..91c71bbc2e561 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
@@ -45,9 +45,6 @@ class WebAssemblyTargetLowering final : public TargetLowering 
{
   WebAssemblyTargetLowering(const TargetMachine &TM,
                             const WebAssemblySubtarget &STI);
 
-  MVT getPointerTy(const DataLayout &DL, uint32_t AS = 0) const override;
-  MVT getPointerMemTy(const DataLayout &DL, uint32_t AS = 0) const override;
-
 private:
   /// Keep a pointer to the WebAssemblySubtarget around so that we can make the
   /// right decision when generating code for different targets.
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td 
b/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
index 608963d588635..169f2301d5272 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
@@ -44,3 +44,11 @@ def : Pat<(select (i32 (setne I32:$cond, 0)), rc:$lhs, 
rc:$rhs),
 def : Pat<(select (i32 (seteq I32:$cond, 0)), rc:$lhs, rc:$rhs),
           (!cast<Instruction>("SELECT_"#rc) rc:$rhs, rc:$lhs, I32:$cond)>;
 }
+
+// Define node for wasm.funcref.to_ptr intrinsic.
+// This will then be combined with a call to the result of the intrinsic
+// to generate a proper funcref call, which is a table.set + a call indirect.
+def WebAssemblyFuncrefToPtr_t : SDTypeProfile<1, 1, [SDTCisPtrTy<0>]>;
+def WebAssemblyFuncrefToPtr   : SdNode<"WebAssemblyISD::FUNCREF_TO_PTR", 
WebAssemblyFuncrefToPtr_t,
+                                []>;
+
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyLowerRefTypesIntPtrConv.cpp 
b/llvm/lib/Target/WebAssembly/WebAssemblyLowerRefTypesIntPtrConv.cpp
deleted file mode 100644
index 2594430d1d8f3..0000000000000
--- a/llvm/lib/Target/WebAssembly/WebAssemblyLowerRefTypesIntPtrConv.cpp
+++ /dev/null
@@ -1,86 +0,0 @@
-//=== WebAssemblyLowerRefTypesIntPtrConv.cpp -
-//                     Lower IntToPtr and PtrToInt on Reference Types   
---===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-///
-/// \file
-/// Lowers IntToPtr and PtrToInt instructions on reference types to
-/// Trap instructions since they have been allowed to operate
-/// on non-integral pointers.
-///
-//===----------------------------------------------------------------------===//
-
-#include "Utils/WebAssemblyTypeUtilities.h"
-#include "WebAssembly.h"
-#include "WebAssemblySubtarget.h"
-#include "llvm/IR/InstIterator.h"
-#include "llvm/Pass.h"
-#include <set>
-
-using namespace llvm;
-
-#define DEBUG_TYPE "wasm-lower-reftypes-intptr-conv"
-
-namespace {
-class WebAssemblyLowerRefTypesIntPtrConv final : public FunctionPass {
-  StringRef getPassName() const override {
-    return "WebAssembly Lower RefTypes Int-Ptr Conversions";
-  }
-
-  bool runOnFunction(Function &MF) override;
-
-public:
-  static char ID; // Pass identification
-  WebAssemblyLowerRefTypesIntPtrConv() : FunctionPass(ID) {}
-};
-} // end anonymous namespace
-
-char WebAssemblyLowerRefTypesIntPtrConv::ID = 0;
-INITIALIZE_PASS(WebAssemblyLowerRefTypesIntPtrConv, DEBUG_TYPE,
-                "WebAssembly Lower RefTypes Int-Ptr Conversions", false, false)
-
-FunctionPass *llvm::createWebAssemblyLowerRefTypesIntPtrConv() {
-  return new WebAssemblyLowerRefTypesIntPtrConv();
-}
-
-bool WebAssemblyLowerRefTypesIntPtrConv::runOnFunction(Function &F) {
-  LLVM_DEBUG(dbgs() << "********** Lower RefTypes IntPtr Convs **********\n"
-                       "********** Function: "
-                    << F.getName() << '\n');
-
-  // This function will check for uses of ptrtoint and inttoptr on reference
-  // types and replace them with a trap instruction.
-  //
-  // We replace the instruction by a trap instruction
-  // and its uses by null in the case of inttoptr and 0 in the
-  // case of ptrtoint.
-  std::set<Instruction *> worklist;
-
-  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
-    PtrToIntInst *PTI = dyn_cast<PtrToIntInst>(&*I);
-    IntToPtrInst *ITP = dyn_cast<IntToPtrInst>(&*I);
-    if (!(PTI && WebAssembly::isWebAssemblyReferenceType(
-                     PTI->getPointerOperand()->getType())) &&
-        !(ITP && WebAssembly::isWebAssemblyReferenceType(ITP->getDestTy())))
-      continue;
-
-    UndefValue *U = UndefValue::get(I->getType());
-    I->replaceAllUsesWith(U);
-
-    Function *TrapIntrin =
-        Intrinsic::getDeclaration(F.getParent(), Intrinsic::debugtrap);
-    CallInst::Create(TrapIntrin, {}, "", I->getIterator());
-
-    worklist.insert(&*I);
-  }
-
-  // erase each instruction replaced by trap
-  for (Instruction *I : worklist)
-    I->eraseFromParent();
-
-  return !worklist.empty();
-}
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp 
b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
index de342e8965736..dab75893d6fc3 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
@@ -88,7 +88,6 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void 
LLVMInitializeWebAssemblyTarget() {
   initializeWebAssemblyDebugFixupPass(PR);
   initializeWebAssemblyPeepholePass(PR);
   initializeWebAssemblyMCLowerPrePassPass(PR);
-  initializeWebAssemblyLowerRefTypesIntPtrConvPass(PR);
   initializeWebAssemblyFixBrTableDefaultsPass(PR);
   initializeWebAssemblyDAGToDAGISelPass(PR);
 }
@@ -626,7 +625,6 @@ void WebAssemblyPassConfig::addPreEmitPass() {
 
 bool WebAssemblyPassConfig::addPreISel() {
   TargetPassConfig::addPreISel();
-  addPass(createWebAssemblyLowerRefTypesIntPtrConv());
   return false;
 }
 
diff --git a/llvm/test/CodeGen/WebAssembly/externref-globalget.ll 
b/llvm/test/CodeGen/WebAssembly/externref-globalget.ll
index cdf98c42439f2..8b84be87d01cb 100644
--- a/llvm/test/CodeGen/WebAssembly/externref-globalget.ll
+++ b/llvm/test/CodeGen/WebAssembly/externref-globalget.ll
@@ -1,6 +1,6 @@
 ; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types | FileCheck %s
 
-%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
+%externref = type target("wasm.externref")
 
 @externref_global = local_unnamed_addr addrspace(1) global %externref undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/externref-globalset.ll 
b/llvm/test/CodeGen/WebAssembly/externref-globalset.ll
index 5bfd673e89fa1..57cfffe82dee0 100644
--- a/llvm/test/CodeGen/WebAssembly/externref-globalset.ll
+++ b/llvm/test/CodeGen/WebAssembly/externref-globalset.ll
@@ -1,6 +1,6 @@
 ; RUN: llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s | FileCheck %s
 
-%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
+%externref = type target("wasm.externref")
 
 @externref_global = local_unnamed_addr addrspace(1) global %externref undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/externref-inttoptr.ll 
b/llvm/test/CodeGen/WebAssembly/externref-inttoptr.ll
index 64f955b6ed0f0..e8ffb1d9a0bc2 100644
--- a/llvm/test/CodeGen/WebAssembly/externref-inttoptr.ll
+++ b/llvm/test/CodeGen/WebAssembly/externref-inttoptr.ll
@@ -1,16 +1,10 @@
-; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types 2>&1 | FileCheck %s
+; RUN: not llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
 
-%externref = type ptr addrspace(10)
+%externref = type target("wasm.externref")
 
 define %externref @int_to_externref(i32 %i) {
   %ref = inttoptr i32 %i to %externref
   ret %externref %ref
 }
 
-
-; CHECK-LABEL: int_to_externref:
-; CHECK-NEXT: .functype       int_to_externref (i32) -> (externref)
-; CHECK-NEXT: .local externref
-; CHECK-NEXT: unreachable
-; CHECK-NEXT: local.get 1
-; CHECK-NEXT: end_function
+# CHECK-ERROR: error: invalid cast opcode for cast from 'i32' to 
'target("wasm.externref")'
\ No newline at end of file
diff --git a/llvm/test/CodeGen/WebAssembly/externref-ptrtoint.ll 
b/llvm/test/CodeGen/WebAssembly/externref-ptrtoint.ll
index 22558796f0624..7520383d06dca 100644
--- a/llvm/test/CodeGen/WebAssembly/externref-ptrtoint.ll
+++ b/llvm/test/CodeGen/WebAssembly/externref-ptrtoint.ll
@@ -1,4 +1,4 @@
-; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types 2>&1 | FileCheck %s
+; RUN: not llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
 
 %externref = type ptr addrspace(10)
 
diff --git a/llvm/test/CodeGen/WebAssembly/externref-tableget.ll 
b/llvm/test/CodeGen/WebAssembly/externref-tableget.ll
index d9ae7c8f6c9b1..1189fc3571902 100644
--- a/llvm/test/CodeGen/WebAssembly/externref-tableget.ll
+++ b/llvm/test/CodeGen/WebAssembly/externref-tableget.ll
@@ -1,6 +1,6 @@
 ; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types | FileCheck %s
 
-%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
+%externref = type target("wasm.externref")
 
 @externref_table = local_unnamed_addr addrspace(1) global [0 x %externref] 
undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/externref-tableset.ll 
b/llvm/test/CodeGen/WebAssembly/externref-tableset.ll
index 37c663869428e..f84ae4dba68eb 100644
--- a/llvm/test/CodeGen/WebAssembly/externref-tableset.ll
+++ b/llvm/test/CodeGen/WebAssembly/externref-tableset.ll
@@ -1,6 +1,6 @@
 ; RUN: llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s | FileCheck %s
 
-%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
+%externref = type target("wasm.externref")
 
 @externref_table = local_unnamed_addr addrspace(1) global [0 x %externref] 
undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/externref-unsized-load.ll 
b/llvm/test/CodeGen/WebAssembly/externref-unsized-load.ll
index 945045d902ef5..98a0e801af387 100644
--- a/llvm/test/CodeGen/WebAssembly/externref-unsized-load.ll
+++ b/llvm/test/CodeGen/WebAssembly/externref-unsized-load.ll
@@ -1,10 +1,10 @@
 ; RUN: not llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
 
-%externref = type ptr addrspace(10)
+%externref = type target("wasm.externref")
 
 define void @load_extern(%externref %ref) {
   %e = load %extern, %externref %ref
   ret void
 }
 
-; CHECK-ERROR: error: loading unsized types is not allowed
+; CHECK-ERROR: error: load operand must be a pointer to a first class type
diff --git a/llvm/test/CodeGen/WebAssembly/externref-unsized-store.ll 
b/llvm/test/CodeGen/WebAssembly/externref-unsized-store.ll
index ca01f69a08ad2..93d82a30c5bf3 100644
--- a/llvm/test/CodeGen/WebAssembly/externref-unsized-store.ll
+++ b/llvm/test/CodeGen/WebAssembly/externref-unsized-store.ll
@@ -1,10 +1,10 @@
 ; RUN: not llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
 
-%externref = type ptr addrspace(10)
+%externref = type target("wasm.funcref")
 
 define void @store_extern(%externref %ref) {
   store %extern undef, %externref %ref
   ret void
 }
 
-; CHECK-ERROR: error: storing unsized types is not allowed
+; CHECK-ERROR: error: store operand must be a pointer
diff --git a/llvm/test/CodeGen/WebAssembly/funcref-call.ll 
b/llvm/test/CodeGen/WebAssembly/funcref-call.ll
index c4eb425244450..397f56acda64d 100644
--- a/llvm/test/CodeGen/WebAssembly/funcref-call.ll
+++ b/llvm/test/CodeGen/WebAssembly/funcref-call.ll
@@ -1,7 +1,9 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
 ; RUN: llc < %s --mtriple=wasm32-unknown-unknown -mattr=+reference-types | 
FileCheck %s
 
-%funcref = type ptr addrspace(20) ;; addrspace 20 is nonintegral
+%funcref = type target("wasm.funcref")
+
+declare ptr @llvm.wasm.funcref.to_ptr(%funcref) nounwind
 
 ; CHECK: .tabletype __funcref_call_table, funcref, 1
 
@@ -18,7 +20,8 @@ define void @call_funcref(%funcref %ref) {
 ; CHECK-NEXT:    ref.null_func
 ; CHECK-NEXT:    table.set __funcref_call_table
 ; CHECK-NEXT:    # fallthrough-return
-  call addrspace(20) void %ref()
+  %refptr = call ptr @llvm.wasm.funcref.to_ptr(%funcref %ref)
+  call void %refptr()
   ret void
 }
 
@@ -40,6 +43,7 @@ define float @call_funcref_with_args(%funcref %ref) {
 ; CHECK-NEXT:    table.set __funcref_call_table
 ; CHECK-NEXT:    local.get 1
 ; CHECK-NEXT:    # fallthrough-return
-  %ret = call addrspace(20) float %ref(double 1.0, i32 2)
+  %refptr = call ptr @llvm.wasm.funcref.to_ptr(%funcref %ref)
+  %ret = call float %refptr(double 1.0, i32 2)
   ret float %ret
 }
diff --git a/llvm/test/CodeGen/WebAssembly/funcref-globalget.ll 
b/llvm/test/CodeGen/WebAssembly/funcref-globalget.ll
index 9aa7fdabfdea9..2634e5a6fc57e 100644
--- a/llvm/test/CodeGen/WebAssembly/funcref-globalget.ll
+++ b/llvm/test/CodeGen/WebAssembly/funcref-globalget.ll
@@ -1,6 +1,6 @@
 ; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types | FileCheck %s
 
-%funcref = type ptr addrspace(20) ;; addrspace 20 is nonintegral
+%funcref = type target("wasm.funcref")
 
 @funcref_global = local_unnamed_addr addrspace(1) global %funcref undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/funcref-globalset.ll 
b/llvm/test/CodeGen/WebAssembly/funcref-globalset.ll
index ca2feb6617996..791e8648537c0 100644
--- a/llvm/test/CodeGen/WebAssembly/funcref-globalset.ll
+++ b/llvm/test/CodeGen/WebAssembly/funcref-globalset.ll
@@ -1,6 +1,6 @@
 ; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types | FileCheck %s
 
-%funcref = type ptr addrspace(20) ;; addrspace 20 is nonintegral
+%funcref = type target("wasm.funcref")
 
 @funcref_global = local_unnamed_addr addrspace(1) global %funcref undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/funcref-table_call.ll 
b/llvm/test/CodeGen/WebAssembly/funcref-table_call.ll
index 74bbc802ac077..dc8cb19d3239a 100644
--- a/llvm/test/CodeGen/WebAssembly/funcref-table_call.ll
+++ b/llvm/test/CodeGen/WebAssembly/funcref-table_call.ll
@@ -1,6 +1,6 @@
 ; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types | FileCheck %s
 
-%funcref = type ptr addrspace(20) ;; addrspace 20 is nonintegral
+%funcref = type target("wasm.funcref")
 
 @funcref_table = local_unnamed_addr addrspace(1) global [0 x %funcref] undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/funcref-tableget.ll 
b/llvm/test/CodeGen/WebAssembly/funcref-tableget.ll
index 3df308c5ddf80..2621dcde6a79b 100644
--- a/llvm/test/CodeGen/WebAssembly/funcref-tableget.ll
+++ b/llvm/test/CodeGen/WebAssembly/funcref-tableget.ll
@@ -1,6 +1,6 @@
 ; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types | FileCheck %s
 
-%funcref = type ptr addrspace(20) ;; addrspace 20 is nonintegral
+%funcref = type target("wasm.funcref")
 
 @funcref_table = local_unnamed_addr addrspace(1) global [0 x %funcref] undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/funcref-tableset.ll 
b/llvm/test/CodeGen/WebAssembly/funcref-tableset.ll
index 98e1b55613d7d..e62eb8a6f2d20 100644
--- a/llvm/test/CodeGen/WebAssembly/funcref-tableset.ll
+++ b/llvm/test/CodeGen/WebAssembly/funcref-tableset.ll
@@ -1,6 +1,6 @@
 ; RUN: llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s | FileCheck %s
 
-%funcref = type ptr addrspace(20) ;; addrspace 20 is nonintegral
+%funcref = type target("wasm.funcref")
 
 @funcref_table = local_unnamed_addr addrspace(1) global [0 x %funcref] undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/ref-null.ll 
b/llvm/test/CodeGen/WebAssembly/ref-null.ll
index af6ddfd8e0814..f2f2a0dad378b 100644
--- a/llvm/test/CodeGen/WebAssembly/ref-null.ll
+++ b/llvm/test/CodeGen/WebAssembly/ref-null.ll
@@ -1,8 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
 ; RUN: llc --mtriple=wasm32-unknown-unknown -mattr=+reference-types < %s | 
FileCheck %s
 
-%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
-%funcref = type ptr addrspace(20)   ;; addrspace 20 is nonintegral
+%externref = type target("wasm.externref")
+%funcref = type target("wasm.funcref")
 
 declare %externref @llvm.wasm.ref.null.extern() nounwind
 declare %funcref @llvm.wasm.ref.null.func() nounwind
diff --git a/llvm/test/CodeGen/WebAssembly/table-copy.ll 
b/llvm/test/CodeGen/WebAssembly/table-copy.ll
index 5c0647ada4ab0..d1387428c4368 100644
--- a/llvm/test/CodeGen/WebAssembly/table-copy.ll
+++ b/llvm/test/CodeGen/WebAssembly/table-copy.ll
@@ -1,6 +1,6 @@
 ; RUN: llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s | FileCheck %s
 
-%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
+%externref = type target("wasm.externref")
 
 @externref_table1 = local_unnamed_addr addrspace(1) global [0 x %externref] 
undef
 @externref_table2 = local_unnamed_addr addrspace(1) global [0 x %externref] 
undef
diff --git a/llvm/test/CodeGen/WebAssembly/table-fill.ll 
b/llvm/test/CodeGen/WebAssembly/table-fill.ll
index 0b78124f038b1..7ed39361a1afd 100644
--- a/llvm/test/CodeGen/WebAssembly/table-fill.ll
+++ b/llvm/test/CodeGen/WebAssembly/table-fill.ll
@@ -1,6 +1,6 @@
 ; RUN: llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s | FileCheck %s
 
-%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
+%externref = type target("wasm.externref")
 
 @externref_table = local_unnamed_addr addrspace(1) global [0 x %externref] 
undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/table-grow.ll 
b/llvm/test/CodeGen/WebAssembly/table-grow.ll
index 614c3400a782b..2b5801ea2fa17 100644
--- a/llvm/test/CodeGen/WebAssembly/table-grow.ll
+++ b/llvm/test/CodeGen/WebAssembly/table-grow.ll
@@ -1,6 +1,6 @@
 ; RUN: llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s | FileCheck %s
 
-%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
+%externref = type target("wasm.externref")
 
 @externref_table = local_unnamed_addr addrspace(1) global [0 x %externref] 
undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/table-size.ll 
b/llvm/test/CodeGen/WebAssembly/table-size.ll
index 42cd2e8a909d7..2b8d0185bb049 100644
--- a/llvm/test/CodeGen/WebAssembly/table-size.ll
+++ b/llvm/test/CodeGen/WebAssembly/table-size.ll
@@ -1,6 +1,6 @@
 ; RUN: llc --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types < %s | FileCheck %s
 
-%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
+%externref = type target("wasm.externref")
 
 @externref_table = local_unnamed_addr addrspace(1) global [0 x %externref] 
undef
 
diff --git a/llvm/test/CodeGen/WebAssembly/table-types.ll 
b/llvm/test/CodeGen/WebAssembly/table-types.ll
index cb5e54e2af230..da04ba35dd58e 100644
--- a/llvm/test/CodeGen/WebAssembly/table-types.ll
+++ b/llvm/test/CodeGen/WebAssembly/table-types.ll
@@ -1,7 +1,7 @@
 ; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false 
-mattr=+reference-types | FileCheck %s
 
-%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral
-%funcref = type ptr addrspace(20)   ;; addrspace 20 is nonintegral
+%externref = type target("wasm.externref")
+%funcref = type target("wasm.funcref")
 
 ; CHECK: .tabletype eref_table, externref
 ; CHECK-NEXT: .globl eref_table
diff --git a/llvm/utils/gn/secondary/llvm/lib/Target/WebAssembly/BUILD.gn 
b/llvm/utils/gn/secondary/llvm/lib/Target/WebAssembly/BUILD.gn
index 2ece91331c5d8..58264892474ee 100644
--- a/llvm/utils/gn/secondary/llvm/lib/Target/WebAssembly/BUILD.gn
+++ b/llvm/utils/gn/secondary/llvm/lib/Target/WebAssembly/BUILD.gn
@@ -53,7 +53,6 @@ static_library("LLVMWebAssemblyCodeGen") {
     "WebAssemblyLateEHPrepare.cpp",
     "WebAssemblyLowerBrUnless.cpp",
     "WebAssemblyLowerEmscriptenEHSjLj.cpp",
-    "WebAssemblyLowerRefTypesIntPtrConv.cpp",
     "WebAssemblyMCInstLower.cpp",
     "WebAssemblyMCLowerPrePass.cpp",
     "WebAssemblyMachineFunctionInfo.cpp",

>From e79363aed798ce22f408fea3c283bb7d6387535a Mon Sep 17 00:00:00 2001
From: Lucile Nihlen <luci.the.r...@gmail.com>
Date: Sun, 26 May 2024 21:19:33 +0000
Subject: [PATCH 2/2] fixup typos

---
 clang/test/CodeGen/WebAssembly/wasm-funcref.c | 40 +++++++++----------
 llvm/lib/CodeGen/ValueTypes.cpp               |  2 +-
 llvm/lib/IR/Type.cpp                          |  2 +-
 .../Target/WebAssembly/WebAssemblyInstrRef.td |  2 +-
 4 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/clang/test/CodeGen/WebAssembly/wasm-funcref.c 
b/clang/test/CodeGen/WebAssembly/wasm-funcref.c
index f01af0db321dd..fa45d87cda74c 100644
--- a/clang/test/CodeGen/WebAssembly/wasm-funcref.c
+++ b/clang/test/CodeGen/WebAssembly/wasm-funcref.c
@@ -8,8 +8,8 @@ typedef int (*fn_t)(int);
 // Null funcref builtin call
 // CHECK-LABEL: @get_null(
 // CHECK-NEXT:  entry:
-// CHECK-NEXT:    [[TMP0:%.*]] = call ptr addrspace(20) 
@llvm.wasm.ref.null.func()
-// CHECK-NEXT:    ret ptr addrspace(20) [[TMP0]]
+// CHECK-NEXT:    [[TMP0:%.*]] = call target("wasm.funcref") 
@llvm.wasm.ref.null.func()
+// CHECK-NEXT:    ret target("wasm.funcref") [[TMP0]]
 //
 funcref_t get_null() {
   return __builtin_wasm_ref_null_func();
@@ -19,8 +19,8 @@ funcref_t get_null() {
 // default return value for builtin is a funcref with function type () -> ().
 // CHECK-LABEL: @get_null_ii(
 // CHECK-NEXT:  entry:
-// CHECK-NEXT:    [[TMP0:%.*]] = call ptr addrspace(20) 
@llvm.wasm.ref.null.func()
-// CHECK-NEXT:    ret ptr addrspace(20) [[TMP0]]
+// CHECK-NEXT:    [[TMP0:%.*]] = call target("wasm.funcref") 
@llvm.wasm.ref.null.func()
+// CHECK-NEXT:    ret target("wasm.funcref") [[TMP0]]
 //
 fn_funcref_t get_null_ii() {
   return (fn_funcref_t) __builtin_wasm_ref_null_func();
@@ -29,10 +29,10 @@ fn_funcref_t get_null_ii() {
 // Identity function for funcref.
 // CHECK-LABEL: @identity(
 // CHECK-NEXT:  entry:
-// CHECK-NEXT:    [[FN_ADDR:%.*]] = alloca ptr addrspace(20), align 4
-// CHECK-NEXT:    store ptr addrspace(20) [[FN:%.*]], ptr [[FN_ADDR]], align 4
-// CHECK-NEXT:    [[TMP0:%.*]] = load ptr addrspace(20), ptr [[FN_ADDR]], 
align 4
-// CHECK-NEXT:    ret ptr addrspace(20) [[TMP0]]
+// CHECK-NEXT:    [[FN_ADDR:%.*]] = alloca target("wasm.funcref"), align 4
+// CHECK-NEXT:    store target("wasm.funcref") [[FN:%.*]], ptr [[FN_ADDR]], 
align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = load target("wasm.funcref"), ptr [[FN_ADDR]], 
align 4
+// CHECK-NEXT:    ret target("wasm.funcref") [[TMP0]]
 //
 funcref_t identity(funcref_t fn) {
   return fn;
@@ -43,10 +43,10 @@ void helper(funcref_t);
 // Pass funcref ref as an argument to a helper function.
 // CHECK-LABEL: @handle(
 // CHECK-NEXT:  entry:
-// CHECK-NEXT:    [[FN_ADDR:%.*]] = alloca ptr addrspace(20), align 4
-// CHECK-NEXT:    store ptr addrspace(20) [[FN:%.*]], ptr [[FN_ADDR]], align 4
-// CHECK-NEXT:    [[TMP0:%.*]] = load ptr addrspace(20), ptr [[FN_ADDR]], 
align 4
-// CHECK-NEXT:    call void @helper(ptr addrspace(20) noundef [[TMP0]])
+// CHECK-NEXT:    [[FN_ADDR:%.*]] = alloca target("wasm.funcref"), align 4
+// CHECK-NEXT:    store target("wasm.funcref") [[FN:%.*]], ptr [[FN_ADDR]], 
align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = load target("wasm.funcref"), ptr [[FN_ADDR]], 
align 4
+// CHECK-NEXT:    call void @helper(target("wasm.funcref") noundef [[TMP0]])
 // CHECK-NEXT:    ret i32 0
 //
 int handle(funcref_t fn) {
@@ -60,8 +60,8 @@ int handle(funcref_t fn) {
 // CHECK-NEXT:    [[FNPTR_ADDR:%.*]] = alloca ptr, align 4
 // CHECK-NEXT:    store ptr [[FNPTR:%.*]], ptr [[FNPTR_ADDR]], align 4
 // CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[FNPTR_ADDR]], align 4
-// CHECK-NEXT:    [[TMP1:%.*]] = addrspacecast ptr [[TMP0]] to ptr 
addrspace(20)
-// CHECK-NEXT:    ret ptr addrspace(20) [[TMP1]]
+// CHECK-NEXT:    [[TMP1:%.*]] = addrspacecast ptr [[TMP0]] to 
target("wasm.funcref")
+// CHECK-NEXT:    ret target("wasm.funcref") [[TMP1]]
 //
 fn_funcref_t get_ref(fn_t fnptr) {
   return (fn_funcref_t) fnptr;
@@ -70,13 +70,13 @@ fn_funcref_t get_ref(fn_t fnptr) {
 // Call funcref
 // CHECK-LABEL: @call_fn(
 // CHECK-NEXT:  entry:
-// CHECK-NEXT:    [[REF_ADDR:%.*]] = alloca ptr addrspace(20), align 4
+// CHECK-NEXT:    [[REF_ADDR:%.*]] = alloca target("wasm.funcref"), align 4
 // CHECK-NEXT:    [[X_ADDR:%.*]] = alloca i32, align 4
-// CHECK-NEXT:    store ptr addrspace(20) [[REF:%.*]], ptr [[REF_ADDR]], align 
4
+// CHECK-NEXT:    store target("wasm.funcref") [[REF:%.*]], ptr [[REF_ADDR]], 
align 4
 // CHECK-NEXT:    store i32 [[X:%.*]], ptr [[X_ADDR]], align 4
-// CHECK-NEXT:    [[TMP0:%.*]] = load ptr addrspace(20), ptr [[REF_ADDR]], 
align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = load target("wasm.funcref"), ptr 
[[REF_ADDR]], align 4
 // CHECK-NEXT:    [[TMP1:%.*]] = load i32, ptr [[X_ADDR]], align 4
-// CHECK-NEXT:    [[CALL:%.*]] = call addrspace(20) i32 [[TMP0]](i32 noundef 
[[TMP1]])
+// CHECK-NEXT:    [[CALL:%.*]] = call i32 [[TMP0]](i32 noundef [[TMP1]])
 // CHECK-NEXT:    ret i32 [[CALL]]
 //
 int call_fn(fn_funcref_t ref, int x) {
@@ -91,8 +91,8 @@ typedef fn_funcref_t (*builtin_refnull_t)();
 // CHECK-NEXT:    [[REFNULL_ADDR:%.*]] = alloca ptr, align 4
 // CHECK-NEXT:    store ptr [[REFNULL:%.*]], ptr [[REFNULL_ADDR]], align 4
 // CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[REFNULL_ADDR]], align 4
-// CHECK-NEXT:    [[CALL:%.*]] = call ptr addrspace(20) [[TMP0]]()
-// CHECK-NEXT:    ret ptr addrspace(20) [[CALL]]
+// CHECK-NEXT:    [[CALL:%.*]] = call target("wasm.funcref") [[TMP0]]()
+// CHECK-NEXT:    ret target("wasm.funcref") [[CALL]]
 //
 fn_funcref_t get_null_fptr(builtin_refnull_t refnull) {
   return refnull();
diff --git a/llvm/lib/CodeGen/ValueTypes.cpp b/llvm/lib/CodeGen/ValueTypes.cpp
index c164bb56313ea..d2d6696f03dbb 100644
--- a/llvm/lib/CodeGen/ValueTypes.cpp
+++ b/llvm/lib/CodeGen/ValueTypes.cpp
@@ -606,7 +606,7 @@ MVT MVT::getVT(Type *Ty, bool HandleUnknown){
       return MVT(MVT::aarch64svcount);
     else if (TargetExtTy->getName() == "wasm.externref")
       return MVT(MVT::externref);
-    else if (TargetExtTy->getName() = "wasm.funcref")
+    else if (TargetExtTy->getName() == "wasm.funcref")
       return MVT(MVT::funcref);
     else if (TargetExtTy->getName().starts_with("spirv."))
       return MVT(MVT::spirvbuiltin);
diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp
index 6d0964cbfecb0..3fdc4310a89fd 100644
--- a/llvm/lib/IR/Type.cpp
+++ b/llvm/lib/IR/Type.cpp
@@ -844,7 +844,7 @@ static TargetTypeInfo getTargetTypeInfo(const TargetExtType 
*Ty) {
                           TargetExtType::HasZeroInit);
 
   // Opaque types in the WebAssembly name space.
-  if (Name.startswith("wasm."))
+  if (Name.starts_with("wasm."))
     return TargetTypeInfo(PointerType::getUnqual(C), 
TargetExtType::HasZeroInit, TargetExtType::CanBeGlobal);
 
   return TargetTypeInfo(Type::getVoidTy(C));
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td 
b/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
index 169f2301d5272..416b86c907806 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrRef.td
@@ -49,6 +49,6 @@ def : Pat<(select (i32 (seteq I32:$cond, 0)), rc:$lhs, 
rc:$rhs),
 // This will then be combined with a call to the result of the intrinsic
 // to generate a proper funcref call, which is a table.set + a call indirect.
 def WebAssemblyFuncrefToPtr_t : SDTypeProfile<1, 1, [SDTCisPtrTy<0>]>;
-def WebAssemblyFuncrefToPtr   : SdNode<"WebAssemblyISD::FUNCREF_TO_PTR", 
WebAssemblyFuncrefToPtr_t,
+def WebAssemblyFuncrefToPtr   : SDNode<"WebAssemblyISD::FUNCREF_TO_PTR", 
WebAssemblyFuncrefToPtr_t,
                                 []>;
 

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to