[PATCH] D138246: [AsmPrinter] Fix Crash when Emitting Global Constant of small bit width when targeting Big Endian arch

Henry Yu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 17 16:18:46 PST 2022


HazyFish created this revision.
HazyFish added a reviewer: Peter.
Herald added a subscriber: hiraditya.
Herald added a project: All.
HazyFish requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

For Big Endian, the function `emitGlobalConstantLargeInt` tries to right shift `Realigned` by an amount `ExtraBitSize` in place. However, if the constant to emit has a bit width less than 64 and the bit width is not a multiple of 8, the shift amount will be greater than the bit width of `Realigned`, which causes assertion error described in issue issue #59055 <https://github.com/llvm/llvm-project/issues/59055>.

This patch fixes the issue by extending the `APInt` to have enough bit width when necessary to avoid the assertion error.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138246

Files:
  llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  llvm/test/CodeGen/AArch64/aarch64_be-global-const.ll


Index: llvm/test/CodeGen/AArch64/aarch64_be-global-const.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/AArch64/aarch64_be-global-const.ll
@@ -0,0 +1,6 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=aarch64_be < %s | FileCheck %s
+
+ at G = global <4 x i1> <i1 true, i1 false, i1 true, i1 true>
+;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
+; CHECK: {{.*}}
Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -3268,6 +3268,9 @@
       ExtraBitsSize = alignTo(ExtraBitsSize, 8);
       ExtraBits = Realigned.getRawData()[0] &
         (((uint64_t)-1) >> (64 - ExtraBitsSize));
+
+      if (BitWidth < 64)
+        Realigned = Realigned.zext(ExtraBitsSize);
       Realigned.lshrInPlace(ExtraBitsSize);
     } else
       ExtraBits = Realigned.getRawData()[BitWidth / 64];


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138246.476265.patch
Type: text/x-patch
Size: 1103 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221118/e9bd45ab/attachment.bin>


More information about the llvm-commits mailing list