From cff0e94befa39e4be07c1b982419407137138ddb Mon Sep 17 00:00:00 2001 From: Martin Fink Date: Tue, 28 Nov 2023 13:50:26 +0000 Subject: [PATCH] add set_tag intrinsic --- .../clang/Basic/BuiltinsWebAssembly.def | 1 + clang/lib/CodeGen/CGBuiltin.cpp | 21 +++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/Basic/BuiltinsWebAssembly.def b/clang/include/clang/Basic/BuiltinsWebAssembly.def index c9d255c4aa8af5..eedf27f149b1c7 100644 --- a/clang/include/clang/Basic/BuiltinsWebAssembly.def +++ b/clang/include/clang/Basic/BuiltinsWebAssembly.def @@ -42,6 +42,7 @@ BUILTIN(__builtin_wasm_segment_new, "v*v*z", "n") BUILTIN(__builtin_wasm_segment_free, "vv*z", "n") BUILTIN(__builtin_wasm_segment_set_tag, "vv*v*z", "n") BUILTIN(__builtin_wasm_untag_ptr, "v*v*", "n") +BUILTIN(__builtin_wasm_tag_ptr, "v*v*v*", "n") // Exception handling builtins. TARGET_BUILTIN(__builtin_wasm_throw, "vIUiv*", "r", "exception-handling") diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index b433effa14eff2..a659456649e52b 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -19851,6 +19851,13 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID, Value *Size = EmitScalarExpr(E->getArg(1)); return Builder.CreateCall(Callee, {Ptr, Size}); } + case WebAssembly::BI__builtin_wasm_segment_set_tag: { + Value *Ptr = EmitScalarExpr(E->getArg(0)); + Value *Tag = EmitScalarExpr(E->getArg(1)); + Value *Size = EmitScalarExpr(E->getArg(2)); + Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_segment_set_tag); + return Builder.CreateCall(Callee, {Ptr, Tag, Size}); + } case WebAssembly::BI__builtin_wasm_untag_ptr: { llvm::Type *IntTy = llvm::Type::getInt64Ty(Builder.getContext()); Value *Ptr = EmitScalarExpr(E->getArg(0)); @@ -19859,12 +19866,18 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID, Value *StrippedPtr = Builder.CreateBinOp(llvm::Instruction::BinaryOps::And, IntPtr, BitMask); return Builder.CreateIntToPtr(StrippedPtr, ConvertType(E->getType())); } - case WebAssembly::BI__builtin_wasm_segment_set_tag: { + case WebAssembly::BI__builtin_wasm_tag_ptr: { + llvm::Type *IntTy = llvm::Type::getInt64Ty(Builder.getContext()); Value *Ptr = EmitScalarExpr(E->getArg(0)); Value *Tag = EmitScalarExpr(E->getArg(1)); - Value *Size = EmitScalarExpr(E->getArg(2)); - Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_segment_set_tag); - return Builder.CreateCall(Callee, {Ptr, Tag, Size}); + Value *IntPtr = Builder.CreateIntToPtr(Ptr, IntTy); + Value *IntTag = Builder.CreateIntToPtr(Tag, IntTy); + Value *BitMask = llvm::ConstantInt::get(IntTy, 0x0000'FFFF'FFFF'FFFF, false); + Value *BitMaskTag = llvm::ConstantInt::get(IntTy, 0xFFFF'0000'0000'0000, false); + IntTag = Builder.CreateBinOp(llvm::Instruction::BinaryOps::And, IntTag, BitMaskTag); + Value *StrippedPtr = Builder.CreateBinOp(llvm::Instruction::BinaryOps::And, IntPtr, BitMask); + Value *TaggedPtr = Builder.CreateBinOp(llvm::Instruction::BinaryOps::Or, StrippedPtr, IntTag); + return Builder.CreateIntToPtr(TaggedPtr, ConvertType(E->getType())); } case WebAssembly::BI__builtin_wasm_throw: { Value *Tag = EmitScalarExpr(E->getArg(0));