Skip to content

Commit

Permalink
changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
gitoleg committed Apr 16, 2024
1 parent 8c9a40a commit 00eb882
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value createNSWSub(mlir::Value lhs, mlir::Value rhs) {
auto op = create<mlir::cir::BinOp>(lhs.getLoc(), lhs.getType(),
mlir::cir::BinOpKind::Sub, lhs, rhs);
op.setHasNoSignedWrap(true);
op.setNoSignedWrap(true);
return op;
}

Expand Down
10 changes: 5 additions & 5 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -934,18 +934,18 @@ def BinOp : CIR_Op<"binop", [Pure,
let results = (outs CIR_AnyType:$result);
let arguments = (ins Arg<BinOpKind, "binop kind">:$kind,
CIR_AnyType:$lhs, CIR_AnyType:$rhs,
UnitAttr:$hasNoUnsignedWrap,
UnitAttr:$hasNoSignedWrap);
UnitAttr:$no_unsigned_wrap,
UnitAttr:$no_signed_wrap);

let assemblyFormat = [{
`(` $kind `,` $lhs `,` $rhs `)`
(`nsw` $hasNoSignedWrap^)?
(`nuw` $hasNoUnsignedWrap^)?
(`nsw` $no_signed_wrap^)?
(`nuw` $no_unsigned_wrap^)?
`:` type($lhs) attr-dict
}];

// Already covered by the traits
let hasVerifier = 0;
let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2769,6 +2769,22 @@ LogicalResult AtomicFetch::verify() {
return mlir::success();
}

LogicalResult BinOp::verify() {
bool noWrap = getNoUnsignedWrap() || getNoSignedWrap();

if (!isa<mlir::cir::IntType>(getType()) && noWrap)
return emitError() << "only operations on integer values may have nsw/nuw flags";

bool noWrapOps = getKind() == mlir::cir::BinOpKind::Add
|| getKind() == mlir::cir::BinOpKind::Sub
|| getKind() == mlir::cir::BinOpKind::Mul;

if (noWrap && !noWrapOps)
return emitError() << "The nsw/nuw flags are applicable to Add, Sub and Mul operations only";

return mlir::success();
}

//===----------------------------------------------------------------------===//
// TableGen'd op method definitions
//===----------------------------------------------------------------------===//
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2012,9 +2012,9 @@ class CIRBinOpLowering : public mlir::OpConversionPattern<mlir::cir::BinOp> {

auto flag = mlir::LLVM::IntegerOverflowFlags::none;

if (op.getHasNoUnsignedWrap())
if (op.getNoUnsignedWrap())
flag = mlir::LLVM::IntegerOverflowFlags::nuw;
else if (op.getHasNoSignedWrap())
else if (op.getNoSignedWrap())
flag = mlir::LLVM::IntegerOverflowFlags::nsw;

return mlir::LLVM::IntegerOverflowFlagsAttr::get(op.getContext(), flag);
Expand Down

0 comments on commit 00eb882

Please sign in to comment.