From e43c3875debcc0a1654dc1d2a0fb2a9fdafdfbe2 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Wed, 17 Apr 2024 16:33:45 +0300 Subject: [PATCH] changes after review --- .../clang/CIR/Dialect/Builder/CIRBaseBuilder.h | 16 ++++++++++------ clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 4 ++-- clang/test/CIR/IR/invalid.cir | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h index a9bdde098b63..cab98d77c135 100644 --- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h +++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h @@ -140,17 +140,21 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { return create(loc, val, dst, _volatile, order); } - mlir::Value createSub(mlir::Value lhs, mlir::Value rhs) { - return createBinop(lhs, mlir::cir::BinOpKind::Sub, rhs); - } - - mlir::Value createNSWSub(mlir::Value lhs, mlir::Value rhs) { + mlir::Value createSub(mlir::Value lhs, mlir::Value rhs, + bool hasNUW = false, bool hasNSW = false) { auto op = create(lhs.getLoc(), lhs.getType(), mlir::cir::BinOpKind::Sub, lhs, rhs); - op.setNoSignedWrap(true); + if (hasNUW) + op.setNoUnsignedWrap(true); + if (hasNSW) + op.setNoSignedWrap(true); return op; } + mlir::Value createNSWSub(mlir::Value lhs, mlir::Value rhs) { + return createSub(lhs, rhs, false, true); + } + //===--------------------------------------------------------------------===// // Cast/Conversion Operators //===--------------------------------------------------------------------===// diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 2673eb6abe01..7026bd5c92b2 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -2777,8 +2777,8 @@ LogicalResult BinOp::verify() { getKind() == mlir::cir::BinOpKind::Mul; if (noWrap && !noWrapOps) - return emitError() << "The nsw/nuw flags are applicable to Add, Sub and " - "Mul operations only"; + return emitError() << + "The nsw/nuw flags are applicable to opcodes: 'add', 'sub' and 'mul'"; return mlir::success(); } diff --git a/clang/test/CIR/IR/invalid.cir b/clang/test/CIR/IR/invalid.cir index ba7758b2ca56..72821f2b9293 100644 --- a/clang/test/CIR/IR/invalid.cir +++ b/clang/test/CIR/IR/invalid.cir @@ -1033,3 +1033,19 @@ cir.func @bad_fetch(%x: !cir.ptr, %y: !cir.float) -> () { %12 = cir.atomic.fetch(xor, %x : !cir.ptr, %y : !cir.float, seq_cst) : !cir.float cir.return } + +// ----- + +cir.func @bad_operands_for_nowrap(%x: !cir.float, %y: !cir.float) { + // expected-error@+1 {{only operations on integer values may have nsw/nuw flags}} + %0 = cir.binop(add, %x, %y) nsw : !cir.float +} + +// ----- + +!u32i = !cir.int + +cir.func @bad_binop_for_nowrap(%x: !u32i, %y: !u32i) { + // expected-error@+1 {{The nsw/nuw flags are applicable to opcodes: 'add', 'sub' and 'mul'}} + %0 = cir.binop(div, %x, %y) nsw : !u32i +}