Skip to content

[LoongArch] Fix out-of-range assert in DAG constant getting #141586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2025

Conversation

heiher
Copy link
Member

@heiher heiher commented May 27, 2025

Fixes #141583

@llvmbot
Copy link
Member

llvmbot commented May 27, 2025

@llvm/pr-subscribers-backend-loongarch

Author: hev (heiher)

Changes

Fixes #141583


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

2 Files Affected:

  • (modified) llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp (+1-1)
  • (modified) llvm/test/CodeGen/LoongArch/bstrins_w.ll (+12)
diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
index 9f5c94ddea44f..7a9ec9f5e96b3 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -4410,7 +4410,7 @@ static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
     LLVM_DEBUG(dbgs() << "Perform OR combine: match pattern 5\n");
     return DAG.getNode(
         LoongArchISD::BSTRINS, DL, ValTy, N0.getOperand(0),
-        DAG.getConstant(CN1->getSExtValue() >> MaskIdx0, DL, ValTy),
+        DAG.getSignedConstant(CN1->getSExtValue() >> MaskIdx0, DL, ValTy),
         DAG.getConstant(ValBits == 32 ? (MaskIdx0 + (MaskLen0 & 31) - 1)
                                       : (MaskIdx0 + MaskLen0 - 1),
                         DL, GRLenVT),
diff --git a/llvm/test/CodeGen/LoongArch/bstrins_w.ll b/llvm/test/CodeGen/LoongArch/bstrins_w.ll
index c59f15bd64112..0fea0470394d2 100644
--- a/llvm/test/CodeGen/LoongArch/bstrins_w.ll
+++ b/llvm/test/CodeGen/LoongArch/bstrins_w.ll
@@ -207,6 +207,18 @@ define i32 @pat8(i32 %c) nounwind {
   ret i32 %or
 }
 
+define i32 @pat9(i32 %a) {
+; CHECK-LABEL: pat9:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    lu12i.w $a1, -8
+; CHECK-NEXT:    ori $a1, $a1, 564
+; CHECK-NEXT:    bstrins.w $a0, $a1, 31, 16
+; CHECK-NEXT:    ret
+  %and = and i32 %a, 65535       ; 0x0000ffff
+  %or = or i32 %and, -2110521344 ; 0x82340000
+  ret i32 %or
+}
+
 ;; Test that bstrins.w is not generated because constant OR operand
 ;; doesn't fit into bits cleared by constant AND operand.
 define i32 @no_bstrins_w(i32 %a) nounwind {

@@ -4410,7 +4410,7 @@ static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
LLVM_DEBUG(dbgs() << "Perform OR combine: match pattern 5\n");
return DAG.getNode(
LoongArchISD::BSTRINS, DL, ValTy, N0.getOperand(0),
DAG.getConstant(CN1->getSExtValue() >> MaskIdx0, DL, ValTy),
DAG.getSignedConstant(CN1->getSExtValue() >> MaskIdx0, DL, ValTy),
Copy link
Contributor

Choose a reason for hiding this comment

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

Do other calls to DAG.getConstant() have the same issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps. I haven't found any others so far, but if I do, I might fix them in this PR or in a follow-up.

Copy link
Member Author

Choose a reason for hiding this comment

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

I came across a few more spots that might be risky, based on the data types and value ranges of getConstant() and getTargetConstant(). I’ll include defensive fixes in this PR, but given the low risk and similarity of the cases, I’m not planning to add separate tests for each one.

@heiher heiher changed the title [LoongArch] Fix assertion failure in performORCombine [LoongArch] Fix out-of-range assert in DAG constant getting May 27, 2025
Copy link
Contributor

@SixWeining SixWeining left a comment

Choose a reason for hiding this comment

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

RV similar fix: #125903

Copy link
Contributor

@wangleiat wangleiat left a comment

Choose a reason for hiding this comment

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

LGTM

@heiher heiher merged commit 54ef0e0 into llvm:main May 28, 2025
11 checks passed
@heiher heiher deleted the issue-141583 branch May 28, 2025 03:06
@llvm-ci
Copy link
Collaborator

llvm-ci commented May 28, 2025

LLVM Buildbot has detected a new failure on builder lldb-aarch64-windows running on linaro-armv8-windows-msvc-05 while building llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/141/builds/9066

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
UNSUPPORTED: lldb-api :: commands/frame/diagnose/dereference-function-return/TestDiagnoseDereferenceFunctionReturn.py (150 of 2168)
UNSUPPORTED: lldb-api :: commands/frame/diagnose/dereference-argument/TestDiagnoseDereferenceArgument.py (151 of 2168)
UNSUPPORTED: lldb-api :: commands/frame/diagnose/inheritance/TestDiagnoseInheritance.py (152 of 2168)
UNSUPPORTED: lldb-api :: commands/frame/diagnose/dereference-this/TestDiagnoseDereferenceThis.py (153 of 2168)
UNSUPPORTED: lldb-api :: commands/frame/diagnose/virtual-method-call/TestDiagnoseDereferenceVirtualMethodCall.py (154 of 2168)
UNSUPPORTED: lldb-api :: commands/frame/diagnose/local-variable/TestLocalVariable.py (155 of 2168)
XFAIL: lldb-api :: commands/frame/language/TestGuessLanguage.py (156 of 2168)
UNSUPPORTED: lldb-api :: commands/frame/select/TestFrameSelect.py (157 of 2168)
PASS: lldb-api :: commands/frame/var-dil/basics/AddressOf/TestFrameVarDILAddressOf.py (158 of 2168)
PASS: lldb-api :: commands/frame/recognizer/TestFrameRecognizer.py (159 of 2168)
FAIL: lldb-api :: commands/frame/var-dil/basics/ArraySubscript/TestFrameVarDILArraySubscript.py (160 of 2168)
******************** TEST 'lldb-api :: commands/frame/var-dil/basics/ArraySubscript/TestFrameVarDILArraySubscript.py' FAILED ********************
Script:
--
C:/Users/tcwg/scoop/apps/python/current/python.exe C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/llvm-project/lldb\test\API\dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./lib --env LLVM_INCLUDE_DIR=C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/include --env LLVM_TOOLS_DIR=C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./bin --arch aarch64 --build-dir C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/lldb-test-build.noindex --lldb-module-cache-dir C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/lldb-test-build.noindex/module-cache-lldb\lldb-api --clang-module-cache-dir C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/lldb-test-build.noindex/module-cache-clang\lldb-api --executable C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./bin/lldb.exe --compiler C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./bin/clang.exe --dsymutil C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./bin/dsymutil.exe --make C:/Users/tcwg/scoop/shims/make.exe --llvm-tools-dir C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./bin --lldb-obj-root C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/tools/lldb --lldb-libs-dir C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./lib --cmake-build-type Release --skip-category=watchpoint C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\llvm-project\lldb\test\API\commands\frame\var-dil\basics\ArraySubscript -p TestFrameVarDILArraySubscript.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision 54ef0e0d8670245ec2ceffc65ef7949e7d6b5a87)
  clang revision 54ef0e0d8670245ec2ceffc65ef7949e7d6b5a87
  llvm revision 54ef0e0d8670245ec2ceffc65ef7949e7d6b5a87
Skipping the following test categories: ['watchpoint', 'libc++', 'libstdcxx', 'dwo', 'dsym', 'gmodules', 'debugserver', 'objc', 'fork', 'pexpect']


--
Command Output (stderr):
--
FAIL: LLDB (C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\build\bin\clang.exe-aarch64) :: test_subscript (TestFrameVarDILArraySubscript.TestFrameVarDILArraySubscript.test_subscript)

XFAIL: LLDB (C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\build\bin\clang.exe-aarch64) :: test_subscript_synthetic (TestFrameVarDILArraySubscript.TestFrameVarDILArraySubscript.test_subscript_synthetic)

======================================================================

FAIL: test_subscript (TestFrameVarDILArraySubscript.TestFrameVarDILArraySubscript.test_subscript)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\llvm-project\lldb\test\API\commands\frame\var-dil\basics\ArraySubscript\TestFrameVarDILArraySubscript.py", line 56, in test_subscript

    self.expect_var_path("int_arr[100]", True, type="int")

  File "C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\llvm-project\lldb\test\API\commands\frame\var-dil\basics\ArraySubscript\TestFrameVarDILArraySubscript.py", line 15, in expect_var_path

    value_dil = super().expect_var_path(expr, value=value, type=type)


sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LoongArch] Assertion failure llvm::isUIntN(BitWidth, val) && "Value is not an N-bit unsigned value"
5 participants