[llvm] Fixed Windows build warnings (PR #68978)

Nikita Kudriavtsev via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 13 04:42:02 PDT 2023


https://github.com/nikita-kud updated https://github.com/llvm/llvm-project/pull/68978

>From dbe31c884062e1f7193cc9165534156a114ab89e Mon Sep 17 00:00:00 2001
From: Nikita Kudriavtsev <nikita.kudriavtsev at intel.com>
Date: Tue, 4 Apr 2023 21:00:01 +0000
Subject: [PATCH 1/2] Fix compilation on Windows

---
 llvm/include/llvm/Support/MathExtras.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index dc095941fdc8a9f..7278c4eb7695887 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -382,7 +382,10 @@ inline uint64_t alignTo(uint64_t Value, uint64_t Align) {
 inline uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align) {
   assert(Align != 0 && (Align & (Align - 1)) == 0 &&
          "Align must be a power of 2");
-  return (Value + Align - 1) & -Align;
+  // Replace unary minus to avoid compilation error on Windows:
+  // "unary minus operator applied to unsigned type, result still unsigned"
+  uint64_t negAlign = (~Align) + 1;
+  return (Value + Align - 1) & negAlign;
 }
 
 /// If non-zero \p Skew is specified, the return value will be a minimal integer

>From 764724d9579807b09116ab4ecdb727dc69798dc9 Mon Sep 17 00:00:00 2001
From: Nikita Kudriavtsev <nikita.kudriavtsev at intel.com>
Date: Fri, 13 Oct 2023 11:26:58 +0000
Subject: [PATCH 2/2] Fix Windows build: avoid narrowing conversion

---
 mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp      | 2 +-
 mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp | 5 +++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp b/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp
index 96d8fceba706617..d245e3322669667 100644
--- a/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp
+++ b/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp
@@ -498,7 +498,7 @@ LogicalResult GPUPrintfOpToVPrintfLowering::matchAndRewrite(
   for (auto [index, arg] : llvm::enumerate(args)) {
     Value ptr = rewriter.create<LLVM::GEPOp>(
         loc, LLVM::LLVMPointerType::get(arg.getType()), tempAlloc,
-        ArrayRef<LLVM::GEPArg>{0, index});
+        ArrayRef<LLVM::GEPArg>{0, static_cast<int32_t>(index)});
     rewriter.create<LLVM::StoreOp>(loc, arg, ptr);
   }
   tempAlloc = rewriter.create<LLVM::BitcastOp>(loc, i8Ptr, tempAlloc);
diff --git a/mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp b/mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp
index 097caf23edfa5dd..f39e02a2ac0ad27 100644
--- a/mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp
+++ b/mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp
@@ -1036,11 +1036,12 @@ Value ConvertLaunchFuncOpToGpuRuntimeCallPattern::generateParamsArray(
   for (const auto &en : llvm::enumerate(arguments)) {
     Value fieldPtr = builder.create<LLVM::GEPOp>(
         loc, getTypeConverter()->getPointerType(argumentTypes[en.index()]),
-        structType, structPtr, ArrayRef<LLVM::GEPArg>{0, en.index()});
+        structType, structPtr,
+        ArrayRef<LLVM::GEPArg>{0, static_cast<int32_t>(en.index())});
     builder.create<LLVM::StoreOp>(loc, en.value(), fieldPtr);
     auto elementPtr = builder.create<LLVM::GEPOp>(
         loc, llvmPointerPointerType, llvmPointerType, arrayPtr,
-        ArrayRef<LLVM::GEPArg>{en.index()});
+        ArrayRef<LLVM::GEPArg>{static_cast<int32_t>(en.index())});
     if (!getTypeConverter()->useOpaquePointers())
       fieldPtr =
           builder.create<LLVM::BitcastOp>(loc, llvmPointerType, fieldPtr);



More information about the llvm-commits mailing list