From: Guanzhong Chen Date: Thu, 18 Jul 2019 17:53:22 +0000 (+0000) Subject: [WebAssembly] Implement __builtin_wasm_tls_base intrinsic X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e47ec1363d1db078e0bd5e3eff3f930f476b6aa8;p=llvm [WebAssembly] Implement __builtin_wasm_tls_base intrinsic Summary: Add `__builtin_wasm_tls_base` so that LeakSanitizer can find the thread-local block and scan through it for memory leaks. Reviewers: tlively, aheejin, sbc100 Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, cfe-commits, llvm-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D64900 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366475 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/IR/IntrinsicsWebAssembly.td b/include/llvm/IR/IntrinsicsWebAssembly.td index 1b892727547..0dc68a5e0c0 100644 --- a/include/llvm/IR/IntrinsicsWebAssembly.td +++ b/include/llvm/IR/IntrinsicsWebAssembly.td @@ -133,4 +133,9 @@ def int_wasm_tls_size : [], [IntrNoMem, IntrSpeculatable]>; +def int_wasm_tls_base : + Intrinsic<[llvm_ptr_ty], + [], + [IntrReadMem]>; + } // TargetPrefix = "wasm" diff --git a/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp b/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp index 26339eaef37..e57e194fdde 100644 --- a/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp +++ b/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp @@ -227,6 +227,23 @@ void WebAssemblyDAGToDAGISel::Select(SDNode *Node) { } break; } + case ISD::INTRINSIC_W_CHAIN: { + unsigned IntNo = cast(Node->getOperand(1))->getZExtValue(); + switch (IntNo) { + case Intrinsic::wasm_tls_base: { + MVT PtrVT = TLI->getPointerTy(CurDAG->getDataLayout()); + assert(PtrVT == MVT::i32 && "only wasm32 is supported for now"); + + MachineSDNode *TLSBase = CurDAG->getMachineNode( + WebAssembly::GLOBAL_GET_I32, DL, MVT::i32, + CurDAG->getTargetExternalSymbol("__tls_base", PtrVT), + Node->getOperand(0)); + ReplaceNode(Node, TLSBase); + return; + } + } + break; + } default: break; diff --git a/test/CodeGen/WebAssembly/tls-general-dynamic.ll b/test/CodeGen/WebAssembly/tls-general-dynamic.ll index 3f6d9d325c6..975e2ce5569 100644 --- a/test/CodeGen/WebAssembly/tls-general-dynamic.ll +++ b/test/CodeGen/WebAssembly/tls-general-dynamic.ll @@ -75,6 +75,15 @@ define i32 @tls_size() { ret i32 %1 } +; CHECK-LABEL: tls_base: +; CHECK-NEXT: .functype tls_base () -> (i32) +define i8* @tls_base() { +; CHECK-NEXT: global.get __tls_base +; CHECK-NEXT: return + %1 = call i8* @llvm.wasm.tls.base() + ret i8* %1 +} + ; CHECK: .type tls,@object ; TLS-NEXT: .section .tbss.tls,"",@ ; NO-TLS-NEXT: .section .bss.tls,"",@ @@ -84,3 +93,4 @@ define i32 @tls_size() { @tls = internal thread_local global i32 0 declare i32 @llvm.wasm.tls.size.i32() +declare i8* @llvm.wasm.tls.base()