[Mlir-commits] [mlir] [mlir][arith] Check for valid IR in BitcastOp::fold. (PR #100743)
Ingo Müller
llvmlistbot at llvm.org
Fri Jul 26 07:12:25 PDT 2024
================
@@ -0,0 +1,46 @@
+//===- ArithOpsFoldersTest.cpp - unit tests for arith op folders ----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Arith/IR/Arith.h"
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/IR/Verifier.h"
+#include "gtest/gtest.h"
+
+using namespace mlir;
+
+namespace {
+// Tests a regression that made `BitcastOp::fold` crash on invalid input IR, see
+// #100743,
+TEST(BitcastOpTest, FoldInteger) {
+ MLIRContext context;
+ context.loadDialect<arith::ArithDialect>();
+ auto loc = UnknownLoc::get(&context);
+ auto module = ModuleOp::create(loc);
+ OpBuilder builder(module.getBodyRegion());
+ Value i32Val = builder.create<arith::ConstantOp>(
+ loc, builder.getI32Type(), builder.getI32IntegerAttr(0));
+ // This would create an invalid op: `bitcast` can't cast different bitwidths.
+ builder.createOrFold<arith::BitcastOp>(loc, builder.getI64Type(), i32Val);
+ ASSERT_TRUE(failed(verify(module)));
+}
+
+// Tests a regression that made `BitcastOp::fold` crash on invalid input IR, see
+// #100743,
+TEST(BitcastOpTest, FoldFloat) {
+ MLIRContext context;
+ context.loadDialect<arith::ArithDialect>();
+ auto loc = UnknownLoc::get(&context);
+ auto module = ModuleOp::create(loc);
+ OpBuilder builder(module.getBodyRegion());
+ Value f32Val = builder.create<arith::ConstantOp>(loc, builder.getF32Type(),
+ builder.getF32FloatAttr(0));
+ // This would create an invalid op: `bitcast` can't cast different bitwidths.
+ builder.createOrFold<arith::BitcastOp>(loc, builder.getF64Type(), f32Val);
+ ASSERT_TRUE(failed(verify(module)));
+}
+} // namespace
----------------
ingomueller-net wrote:
I *think* I can't trigger the assert with a list test because there the input IR would be valid.
https://github.com/llvm/llvm-project/pull/100743
More information about the Mlir-commits
mailing list