[llvm] [SPIRV] Fix assert in `getOrCreateBaseRegister` for `i32 -1` (PR #114630)

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 1 17:59:51 PDT 2024


https://github.com/bogner created https://github.com/llvm/llvm-project/pull/114630

When trying to create a const inst from a 32 bit signed value, we don't want to sign-extend it to 64 bits, as the resulting value won't actually fit in an `i32` if it was negative.

This fixes crashes in the following two tests after the APInt constructor asserts were enabled in #114539:
```
Failed Tests (2):
  LLVM :: CodeGen/SPIRV/transcoding/RelationalOperators.ll
  LLVM :: CodeGen/SPIRV/uitofp-with-bool.ll
```

>From fd3fd315b0621ad828fd2cdddc0cb9b25b17cdc9 Mon Sep 17 00:00:00 2001
From: Justin Bogner <mail at justinbogner.com>
Date: Fri, 1 Nov 2024 17:49:46 -0700
Subject: [PATCH] [SPIRV] Fix assert in `getOrCreateBaseRegister` for `i32 -1`

When trying to create a const inst from a 32 bit signed value, we don't
want to sign-extend it to 64 bits, as the resulting value won't actually
fit in an `i32` if it was negative.

This fixes crashes in the following two tests after the APInt constructor
asserts were enabled in #114539:
```
Failed Tests (2):
  LLVM :: CodeGen/SPIRV/transcoding/RelationalOperators.ll
  LLVM :: CodeGen/SPIRV/uitofp-with-bool.ll
```
---
 llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index 62bd8d1f9d2433..af0df4d6e5d563 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -400,7 +400,7 @@ Register SPIRVGlobalRegistry::getOrCreateBaseRegister(
   }
   assert(Type->getOpcode() == SPIRV::OpTypeInt);
   SPIRVType *SpvBaseType = getOrCreateSPIRVIntegerType(BitWidth, I, TII);
-  return getOrCreateConstInt(Val->getUniqueInteger().getSExtValue(), I,
+  return getOrCreateConstInt(Val->getUniqueInteger().getZExtValue(), I,
                              SpvBaseType, TII, ZeroAsNull);
 }
 



More information about the llvm-commits mailing list