[llvm] 78e8797 - [WebAssembly] Disable offset folding for function addresses
Heejin Ahn via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 12 13:41:07 PDT 2021
Author: Heejin Ahn
Date: 2021-08-12T13:40:41-07:00
New Revision: 78e87970af888bbbd5652c31f3a8454e8e9dd5b8
URL: https://github.com/llvm/llvm-project/commit/78e87970af888bbbd5652c31f3a8454e8e9dd5b8
DIFF: https://github.com/llvm/llvm-project/commit/78e87970af888bbbd5652c31f3a8454e8e9dd5b8.diff
LOG: [WebAssembly] Disable offset folding for function addresses
Wasm does not support function addresses with offsets, but isel can
generate folded SDValues in the form of (@func + offset) without this
patch.
Fixes https://bugs.llvm.org/show_bug.cgi?id=43133.
Reviewed By: dschuff, sbc100
Differential Revision: https://reviews.llvm.org/D107940
Added:
llvm/test/CodeGen/WebAssembly/function-addr-offset.ll
Modified:
llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
Removed:
################################################################################
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
index 31aee898033a2..927b83e8a067c 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
@@ -770,6 +770,13 @@ bool WebAssemblyTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
(ExtT == MVT::v2i64 && MemT == MVT::v2i32);
}
+bool WebAssemblyTargetLowering::isOffsetFoldingLegal(
+ const GlobalAddressSDNode *GA) const {
+ // Wasm doesn't support function addresses with offsets
+ const GlobalValue *GV = GA->getGlobal();
+ return isa<Function>(GV) ? false : TargetLowering::isOffsetFoldingLegal(GA);
+}
+
EVT WebAssemblyTargetLowering::getSetCCResultType(const DataLayout &DL,
LLVMContext &C,
EVT VT) const {
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
index 0e8f0df19fdc8..748b771d930f0 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
@@ -102,6 +102,7 @@ class WebAssemblyTargetLowering final : public TargetLowering {
bool *Fast) const override;
bool isIntDivCheap(EVT VT, AttributeList Attr) const override;
bool isVectorLoadExtDesirable(SDValue ExtVal) const override;
+ bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override;
EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
EVT VT) const override;
bool getTgtMemIntrinsic(IntrinsicInfo &Info, const CallInst &I,
diff --git a/llvm/test/CodeGen/WebAssembly/function-addr-offset.ll b/llvm/test/CodeGen/WebAssembly/function-addr-offset.ll
new file mode 100644
index 0000000000000..f7c9285ab4ce0
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/function-addr-offset.ll
@@ -0,0 +1,35 @@
+; RUN: llc < %s -verify-machineinstrs | FileCheck %s
+
+; Wasm does not currently support function addresses with offsets, so we
+; shouldn't try to create a folded SDNode like (function + offset). This is a
+; regression test for the folding bug and this should not crash in MCInstLower.
+
+target triple = "wasm32-unknown-unknown"
+
+; 'hidden' here should be present to reproduce the bug
+declare hidden void @ham(i8*)
+
+define void @bar(i8* %ptr) {
+bb1:
+ br i1 undef, label %bb3, label %bb2
+
+bb2:
+ ; While lowering this switch, isel creates (@ham + 1) expression as a course
+ ; of range optimization for switch, and tries to fold the expression, but
+ ; wasm does not support with function addresses with offsets. This folding
+ ; should be disabled.
+ ; CHECK: i32.const ham
+ ; CHECK-NEXT: i32.const 1
+ ; CHECK-NEXT: i32.add
+ switch i32 ptrtoint (void (i8*)* @ham to i32), label %bb4 [
+ i32 -1, label %bb3
+ i32 0, label %bb3
+ ]
+
+bb3:
+ unreachable
+
+bb4:
+ %tmp = load i8, i8* %ptr
+ unreachable
+}
More information about the llvm-commits
mailing list