Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
40 changes: 40 additions & 0 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
return create<cir::ConstantOp>(loc, attr.getType(), attr);
}

cir::ConstantOp getConstantInt(mlir::Location loc, mlir::Type ty,
int64_t value) {
return getConstant(loc, cir::IntAttr::get(ty, value));
}

// Creates constant null value for integral type ty.
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc) {
return getConstant(loc, getZeroInitAttr(ty));
}

cir::ConstantOp getBool(bool state, mlir::Location loc) {
return create<cir::ConstantOp>(loc, getBoolTy(), getCIRBoolAttr(state));
}
Expand Down Expand Up @@ -68,6 +78,36 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
getContext(), mlir::cast<cir::PointerType>(type), valueAttr);
}

mlir::TypedAttr getConstNullPtrAttr(mlir::Type t) {
assert(mlir::isa<cir::PointerType>(t) && "expected cir.ptr");
return getConstPtrAttr(t, 0);
}

mlir::TypedAttr getZeroAttr(mlir::Type t) {
return cir::ZeroAttr::get(getContext(), t);
}

mlir::TypedAttr getZeroInitAttr(mlir::Type ty) {
if (mlir::isa<cir::IntType>(ty))
return cir::IntAttr::get(ty, 0);
if (auto fltType = mlir::dyn_cast<cir::SingleType>(ty))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let merge the zero-init change first and rebase this one to get the changes there.

return cir::FPAttr::getZero(fltType);
if (auto fltType = mlir::dyn_cast<cir::DoubleType>(ty))
return cir::FPAttr::getZero(fltType);
if (auto fltType = mlir::dyn_cast<cir::FP16Type>(ty))
return cir::FPAttr::getZero(fltType);
if (auto fltType = mlir::dyn_cast<cir::BF16Type>(ty))
return cir::FPAttr::getZero(fltType);
if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty))
return getZeroAttr(arrTy);
if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty))
return getConstNullPtrAttr(ptrTy);
if (mlir::isa<cir::BoolType>(ty)) {
return getCIRBoolAttr(false);
}
llvm_unreachable("Zero initializer for given type is NYI");
}

mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType,
mlir::Type type, llvm::StringRef name,
mlir::IntegerAttr alignment) {
Expand Down
25 changes: 25 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRDialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@
#include "clang/CIR/Interfaces/CIRLoopOpInterface.h"
#include "clang/CIR/Interfaces/CIROpInterfaces.h"

namespace mlir {
namespace OpTrait {

namespace impl {
// These functions are out-of-line implementations of the methods in the
// corresponding trait classes. This avoids them being template
// instantiated/duplicated.
LogicalResult verifySameFirstOperandAndResultType(Operation *op);
} // namespace impl

/// This class provides verification for ops that are known to have the same
/// first operand and result type.
///
template <typename ConcreteType>
class SameFirstOperandAndResultType
: public TraitBase<ConcreteType, SameFirstOperandAndResultType> {
public:
static llvm::LogicalResult verifyTrait(Operation *op) {
return impl::verifySameFirstOperandAndResultType(op);
}
};

} // namespace OpTrait
} // namespace mlir

// TableGen'erated files for MLIR dialects require that a macro be defined when
// they are included. GET_OP_CLASSES tells the file to define the classes for
// the operations of that dialect.
Expand Down
41 changes: 41 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ class LLVMLoweringInfo {
class CIR_Op<string mnemonic, list<Trait> traits = []> :
Op<CIR_Dialect, mnemonic, traits>, LLVMLoweringInfo;

//===----------------------------------------------------------------------===//
// CIR Op Traits
//===----------------------------------------------------------------------===//

def SameFirstOperandAndResultType :
NativeOpTrait<"SameFirstOperandAndResultType">;

//===----------------------------------------------------------------------===//
// CastOp
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -229,6 +236,40 @@ def CastOp : CIR_Op<"cast",
let hasFolder = 1;
}


//===----------------------------------------------------------------------===//
// PtrStrideOp
//===----------------------------------------------------------------------===//

def PtrStrideOp : CIR_Op<"ptr_stride",
[Pure, SameFirstOperandAndResultType]> {
let summary = "Pointer access with stride";
let description = [{
Given a base pointer as first operand, provides a new pointer after applying
a stride (second operand).

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

let arguments = (ins CIR_PointerType:$base, PrimitiveInt:$stride);
let results = (outs CIR_PointerType:$result);

let assemblyFormat = [{
`(` $base `:` qualified(type($base)) `,` $stride `:`
qualified(type($stride)) `)` `,` qualified(type($result)) attr-dict
}];

let extraClassDeclaration = [{
// Get type pointed by the base pointer.
mlir::Type getElementTy() {
return mlir::cast<cir::PointerType>(getBase().getType()).getPointee();
}
}];
}

//===----------------------------------------------------------------------===//
// ConstantOp
//===----------------------------------------------------------------------===//
Expand Down
40 changes: 40 additions & 0 deletions clang/include/clang/CIR/LoweringHelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//====- LoweringHelpers.h - Lowering helper functions ---------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file declares helper functions for lowering from CIR to LLVM or MLIR.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_CIR_LOWERINGHELPERS_H
#define LLVM_CLANG_CIR_LOWERINGHELPERS_H

#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/Transforms/DialectConversion.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"

mlir::DenseElementsAttr
convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr, mlir::Type type);

template <typename StorageTy> StorageTy getZeroInitFromType(mlir::Type ty);
template <> mlir::APInt getZeroInitFromType(mlir::Type ty);
template <> mlir::APFloat getZeroInitFromType(mlir::Type ty);

template <typename AttrTy, typename StorageTy>
void convertToDenseElementsAttrImpl(cir::ConstArrayAttr attr,
llvm::SmallVectorImpl<StorageTy> &values);

template <typename AttrTy, typename StorageTy>
mlir::DenseElementsAttr
convertToDenseElementsAttr(cir::ConstArrayAttr attr,
const llvm::SmallVectorImpl<int64_t> &dims,
mlir::Type type);

std::optional<mlir::Attribute>
lowerConstArrayAttr(cir::ConstArrayAttr constArr,
const mlir::TypeConverter *converter);

#endif
10 changes: 10 additions & 0 deletions clang/lib/CIR/CodeGen/Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,23 @@ class Address {
return pointerAndKnownNonNull.getPointer();
}

mlir::Type getType() const {
assert(mlir::cast<cir::PointerType>(
pointerAndKnownNonNull.getPointer().getType())
.getPointee() == elementType);

return mlir::cast<cir::PointerType>(getPointer().getType());
}

mlir::Type getElementType() const {
assert(isValid());
assert(mlir::cast<cir::PointerType>(
pointerAndKnownNonNull.getPointer().getType())
.getPointee() == elementType);
return elementType;
}

clang::CharUnits getAlignment() const { return alignment; }
};

} // namespace clang::CIRGen
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {

bool isInt(mlir::Type i) { return mlir::isa<cir::IntType>(i); }

bool isInt8Ty(mlir::Type i) {
return i == typeCache.UInt8Ty || i == typeCache.SInt8Ty;
}

// Creates constant nullptr for pointer type ty.
cir::ConstantOp getNullPtr(mlir::Type ty, mlir::Location loc) {
assert(!cir::MissingFeatures::targetCodeGenInfoGetNullPointer());
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ void CIRGenFunction::emitExprAsInit(const Expr *init, const ValueDecl *d,
return;
}
case cir::TEK_Aggregate:
cgm.errorNYI(init->getSourceRange(), "emitExprAsInit: aggregate type");
emitAggExpr(init, AggValueSlot::forLValue(lvalue));
return;
}
llvm_unreachable("bad evaluation kind");
Expand Down
Loading