Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,9 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {

cir::PtrStrideOp
createPtrStride(mlir::Location loc, mlir::Value base, mlir::Value stride,
std::optional<GEPNoWrapFlags> flags = std::nullopt) {
std::optional<CIR_GEPNoWrapFlags> flags = std::nullopt) {
return cir::PtrStrideOp::create(*this, loc, base.getType(), base, stride,
flags.value_or(GEPNoWrapFlags::none));
flags.value_or(CIR_GEPNoWrapFlags::none));
}

cir::CallOp createCallOp(mlir::Location loc,
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIREnumAttr.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

include "mlir/IR/EnumAttr.td"

class CIR_I32BitEnum<string name, string summary, list<BitEnumCaseBase> cases>
: I32BitEnum<name, summary, cases> {
let cppNamespace = "::cir";
}

class CIR_I32EnumAttr<string name, string summary, list<I32EnumAttrCase> cases>
: I32EnumAttr<name, summary, cases> {
let cppNamespace = "::cir";
Expand Down
30 changes: 18 additions & 12 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,22 @@ def CIR_PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> {
//===----------------------------------------------------------------------===//
// PtrStrideOp
//===----------------------------------------------------------------------===//
def GEPNone : I32BitEnumCaseNone<"none">;
def GEPInboundsFlag : I32BitEnumCaseBit<"inboundsFlag", 0, "inbounds_flag">;
def GEPNusw : I32BitEnumCaseBit<"nusw", 1>;
def GEPNuw : I32BitEnumCaseBit<"nuw", 2>;
def GEPInbounds : BitEnumCaseGroup<"inbounds", [GEPInboundsFlag, GEPNusw]>;

def GEPNoWrapFlags
: I32BitEnum<"GEPNoWrapFlags", "::cir::GEPNoWrapFlags",
[GEPNone, GEPInboundsFlag, GEPNusw, GEPNuw, GEPInbounds]> {
def CIR_GEPNone : I32BitEnumCaseNone<"none">;
def CIR_GEPInboundsFlag : I32BitEnumCaseBit<"inboundsFlag", 0, "inbounds_flag">;
def CIR_GEPNusw : I32BitEnumCaseBit<"nusw", 1>;
def CIR_GEPNuw : I32BitEnumCaseBit<"nuw", 2>;
def CIR_GEPInbounds
: BitEnumCaseGroup<"inbounds", [CIR_GEPInboundsFlag, CIR_GEPNusw]>;

def CIR_GEPNoWrapFlags
: CIR_I32BitEnum<"CIR_GEPNoWrapFlags", "::cir::CIR_GEPNoWrapFlags",
[CIR_GEPNone, CIR_GEPInboundsFlag, CIR_GEPNusw, CIR_GEPNuw,
CIR_GEPInbounds]> {
let cppNamespace = "::cir";
let printBitEnumPrimaryGroups = 1;
}

def GEPNoWrapFlagsProp : EnumProp<GEPNoWrapFlags> {
def CIR_GEPNoWrapFlagsProp : EnumProp<CIR_GEPNoWrapFlags> {
let defaultValue = interfaceType#"::none";
}

Expand All @@ -413,14 +415,18 @@ def CIR_PtrStrideOp : CIR_Op<"ptr_stride",[

```mlir
%3 = cir.const 0 : i32
%4 = cir.ptr_stride(%2 : !cir.ptr<i32>, %3 : i32), !cir.ptr<i32>

%4 = cir.ptr_stride(%2 : !cir.ptr<i32>, %3 : i32), !cir.ptr<i32>

%4 = cir.ptr_stride(%2 : !cir.ptr<i32>, %3 : i32, inbounds), !cir.ptr<i32>

%4 = cir.ptr_stride(%2 : !cir.ptr<i32>, %3 : i32, inbounds|nuw), !cir.ptr<i32>

```
}];

let arguments = (ins CIR_PointerType:$base, CIR_AnyFundamentalIntType:$stride,
GEPNoWrapFlagsProp:$noWrapFlags);
CIR_GEPNoWrapFlagsProp:$noWrapFlags);

let results = (outs CIR_PointerType:$result);

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2837,9 +2837,9 @@ mlir::Value CIRGenFunction::emitCheckedInBoundsGEP(
builder.create<cir::PtrStrideOp>(CGM.getLoc(Loc), PtrTy, Ptr, IdxList[0]);
// If the pointer overflow sanitizer isn't enabled, do nothing.
if (!SanOpts.has(SanitizerKind::PointerOverflow)) {
cir::GEPNoWrapFlags nwFlags = cir::GEPNoWrapFlags::inbounds;
cir::CIR_GEPNoWrapFlags nwFlags = cir::CIR_GEPNoWrapFlags::inbounds;
if (!SignedIndices && !IsSubtraction)
nwFlags = nwFlags | cir::GEPNoWrapFlags::nuw;
nwFlags = nwFlags | cir::CIR_GEPNoWrapFlags::nuw;
return builder.create<cir::PtrStrideOp>(CGM.getLoc(Loc), PtrTy, Ptr,
IdxList[0], nwFlags);
}
Expand Down
21 changes: 20 additions & 1 deletion clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ void walkRegionSkipping(mlir::Region &region,
});
}

/// Convert from a CIR PtrStrideOp kind to an LLVM IR equivalent of GEP.
mlir::LLVM::GEPNoWrapFlags
convertPtrStrideKindToGEPFlags(cir::CIR_GEPNoWrapFlags flags) {
static std::unordered_map<cir::CIR_GEPNoWrapFlags, mlir::LLVM::GEPNoWrapFlags>
mp = {
{cir::CIR_GEPNoWrapFlags::none, mlir::LLVM::GEPNoWrapFlags::none},
{cir::CIR_GEPNoWrapFlags::inbounds,
mlir::LLVM::GEPNoWrapFlags::inbounds},
{cir::CIR_GEPNoWrapFlags::inboundsFlag,
mlir::LLVM::GEPNoWrapFlags::inboundsFlag},
{cir::CIR_GEPNoWrapFlags::nusw, mlir::LLVM::GEPNoWrapFlags::nusw},
{cir::CIR_GEPNoWrapFlags::nuw, mlir::LLVM::GEPNoWrapFlags::nuw},
};
mlir::LLVM::GEPNoWrapFlags x = mlir::LLVM::GEPNoWrapFlags::none;
for (auto [key, _] : mp)
x = x | mp.at(flags & key);
return x;
}

/// Convert from a CIR comparison kind to an LLVM IR integral comparison kind.
mlir::LLVM::ICmpPredicate convertCmpKindToICmpPredicate(cir::CmpOpKind kind,
bool isSigned) {
Expand Down Expand Up @@ -1025,7 +1044,7 @@ mlir::LogicalResult CIRToLLVMPtrStrideOpLowering::matchAndRewrite(
}
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(
ptrStrideOp, resultTy, elementTy, adaptor.getBase(), index,
static_cast<mlir::LLVM::GEPNoWrapFlags>(adaptor.getNoWrapFlags()));
convertPtrStrideKindToGEPFlags(adaptor.getNoWrapFlags()));
return mlir::success();
}

Expand Down
Loading