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
176 changes: 176 additions & 0 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//===-- CIRBaseBuilder.h - CIRBuilder implementation -----------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_CIRBASEBUILDER_H
#define LLVM_CLANG_LIB_CIRBASEBUILDER_H

#include "clang/AST/Decl.h"
#include "clang/AST/Type.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/Dialect/IR/FPEnv.h"

#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Location.h"
#include "mlir/IR/Types.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/FloatingPointMode.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/ErrorHandling.h"
#include <cassert>
#include <optional>
#include <string>

namespace cir {

class CIRBaseBuilderTy : public mlir::OpBuilder {

public:
CIRBaseBuilderTy(mlir::MLIRContext &C) : mlir::OpBuilder(&C) {}


mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ,
const llvm::APInt &val) {
return create<mlir::cir::ConstantOp>(loc, typ,
getAttr<mlir::cir::IntAttr>(typ, val));
}


mlir::Value createNot(mlir::Value value) {
return create<mlir::cir::UnaryOp>(value.getLoc(), value.getType(),
mlir::cir::UnaryOpKind::Not, value);
}

mlir::Value createBinop(mlir::Value lhs, mlir::cir::BinOpKind kind,
const llvm::APInt &rhs) {
return create<mlir::cir::BinOp>(
lhs.getLoc(), lhs.getType(), kind, lhs,
getConstAPInt(lhs.getLoc(), lhs.getType(), rhs));
}

mlir::Value createBinop(mlir::Value lhs, mlir::cir::BinOpKind kind,
mlir::Value rhs) {
return create<mlir::cir::BinOp>(lhs.getLoc(), lhs.getType(), kind, lhs,
rhs);
}

mlir::Value createShift(mlir::Value lhs, const llvm::APInt &rhs,
bool isShiftLeft) {
return create<mlir::cir::ShiftOp>(
lhs.getLoc(), lhs.getType(), lhs,
getConstAPInt(lhs.getLoc(), lhs.getType(), rhs), isShiftLeft);
}

mlir::Value createShift(mlir::Value lhs, unsigned bits, bool isShiftLeft) {
auto width = lhs.getType().dyn_cast<mlir::cir::IntType>().getWidth();
auto shift = llvm::APInt(width, bits);
return createShift(lhs, shift, isShiftLeft);
}

mlir::Value createShiftLeft(mlir::Value lhs, unsigned bits) {
return createShift(lhs, bits, true);
}

mlir::Value createShiftRight(mlir::Value lhs, unsigned bits) {
return createShift(lhs, bits, false);
}

mlir::Value createLowBitsSet(mlir::Location loc, unsigned size,
unsigned bits) {
auto val = llvm::APInt::getLowBitsSet(size, bits);
auto typ = mlir::cir::IntType::get(getContext(), size, false);
return getConstAPInt(loc, typ, val);
}

mlir::Value createAnd(mlir::Value lhs, llvm::APInt rhs) {
auto val = getConstAPInt(lhs.getLoc(), lhs.getType(), rhs);
return createBinop(lhs, mlir::cir::BinOpKind::And, val);
}

mlir::Value createAnd(mlir::Value lhs, mlir::Value rhs) {
return createBinop(lhs, mlir::cir::BinOpKind::And, rhs);
}

mlir::Value createOr(mlir::Value lhs, llvm::APInt rhs) {
auto val = getConstAPInt(lhs.getLoc(), lhs.getType(), rhs);
return createBinop(lhs, mlir::cir::BinOpKind::Or, val);
}

mlir::Value createOr(mlir::Value lhs, mlir::Value rhs) {
return createBinop(lhs, mlir::cir::BinOpKind::Or, rhs);
}

//===--------------------------------------------------------------------===//
// Cast/Conversion Operators
//===--------------------------------------------------------------------===//

mlir::Value createCast(mlir::cir::CastKind kind, mlir::Value src,
mlir::Type newTy) {
if (newTy == src.getType())
return src;
return create<mlir::cir::CastOp>(src.getLoc(), newTy, kind, src);
}

mlir::Value createIntCast(mlir::Value src, mlir::Type newTy) {
return create<mlir::cir::CastOp>(src.getLoc(), newTy,
mlir::cir::CastKind::integral, src);
}

mlir::Value createIntToPtr(mlir::Value src, mlir::Type newTy) {
return create<mlir::cir::CastOp>(src.getLoc(), newTy,
mlir::cir::CastKind::int_to_ptr, src);
}

mlir::Value createPtrToInt(mlir::Value src, mlir::Type newTy) {
return create<mlir::cir::CastOp>(src.getLoc(), newTy,
mlir::cir::CastKind::ptr_to_int, src);
}

// TODO(cir): the following function was introduced to keep in sync with LLVM
// codegen. CIR does not have "zext" operations. It should eventually be
// renamed or removed. For now, we just add whatever cast is required here.
mlir::Value createZExtOrBitCast(mlir::Location loc, mlir::Value src,
mlir::Type newTy) {
auto srcTy = src.getType();

if (srcTy == newTy)
return src;

if (srcTy.isa<mlir::cir::BoolType>() && newTy.isa<mlir::cir::IntType>())
return createBoolToInt(src, newTy);

llvm_unreachable("unhandled extension cast");
}

mlir::Value createBoolToInt(mlir::Value src, mlir::Type newTy) {
return createCast(mlir::cir::CastKind::bool_to_int, src, newTy);
}

mlir::Value createBitcast(mlir::Value src, mlir::Type newTy) {
return createCast(mlir::cir::CastKind::bitcast, src, newTy);
}

mlir::Value createBitcast(mlir::Location loc, mlir::Value src,
mlir::Type newTy) {
if (newTy == src.getType())
return src;
return create<mlir::cir::CastOp>(loc, newTy, mlir::cir::CastKind::bitcast,
src);
}
};

} // namespace cir
#endif
29 changes: 29 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,33 @@ def GlobalCtorAttr : CIR_Attr<"GlobalCtor", "globalCtor"> {
];
let skipDefaultBuilders = 1;
}

def BitfieldInfoAttr : CIR_Attr<"BitfieldInfo", "bitfieldInfo"> {
let parameters = (ins "StringAttr":$name,
"Type":$eltType,
"uint64_t":$storageSize,
"uint64_t":$size,
"uint64_t":$offset,
"bool":$isSigned);

let assemblyFormat = [{ `<`
`(` `name` `=` $name `)`
`(` `size` $size `)`
`(` `storage` $eltType $storageSize `)`
`(` `offset` $offset `)`
`(` `signed` `=` $isSigned `)` `>` }];

let builders = [
AttrBuilder<(ins "StringRef":$name,
"Type":$eltType,
"uint64_t":$storageSize,
"uint64_t":$size,
"uint64_t":$offset,
"bool":$isSigned
), [{
return $_get($_ctxt, StringAttr::get($_ctxt, name), eltType, storageSize, size, offset, isSigned);
}]>
];
}

#endif // MLIR_CIR_DIALECT_CIR_ATTRS
101 changes: 101 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,107 @@ def VTableAddrPointOp : CIR_Op<"vtable.address_point",
let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// SetBitfieldOp
//===----------------------------------------------------------------------===//

def SetBitfieldOp : CIR_Op<"set_bitfield"> {
let summary = "Set a bitfield";
let description = [{
The `cir.set_bitfield` operation provides a store-like access to
a bit field of a record.

It expects an address of a storage where to store, a value being stored,
a type of a bitfield, a name of a bit field, a pointer to a storage in the
base record, a size of the storage, a size the bit field, an offset
of the bit field and a sign. Returns a value being stored.

Example:
```mlir
// Suppose we have a struct with multiple bitfields stored in
// different storages. The `cir.set_bitfield` operation sets the value
// of the bitfield

// Sets the bitfield with name a1.
%6 = "cir.set_bitfield"(%5, %3) <{BitfieldInfo = #cir.bitfieldInfo<(name = "a1") (size 4) (storage !u64i 64) (offset 0) (signed = true)>}> : (!cir.ptr<!u64i>, !s32i) -> !s32i
```
}];

let arguments = (ins
AnyType:$dst,
AnyType:$src,
BitfieldInfoAttr:$BitfieldInfo
);

let results = (outs CIR_IntType:$result);

let builders = [
OpBuilder<(ins "Type":$type,
"Value":$dst,
"Type":$eltType,
"Value":$src,
"StringRef":$name,
"unsigned":$storageSize,
"unsigned":$size,
"unsigned":$offset,
"bool":$isSigned
),
[{
BitfieldInfoAttr info = BitfieldInfoAttr::get($_builder.getContext(), name, eltType, storageSize, size, offset, isSigned);
build($_builder, $_state, type, dst, src, info);
}]>
];
}

//===----------------------------------------------------------------------===//
// GetBitfieldOp
//===----------------------------------------------------------------------===//

def GetBitfieldOp : CIR_Op<"get_bitfield"> {
let summary = "Get a bitfield";
let description = [{
The `cir.get_bitfield` operation provides a load-like access to
a bit field of a record.

It expects a name if a bit field, a pointer to a storage in the
base record, a size of the storage, a size the bit field, an offset
of the bit field and a sign.

Example:
```mlir
// Suppose we have a struct with multiple bitfields stored in
// different storages. The `cir.get_bitfield` operation gets the value
// of the bitfield

// Get the address of the bitfield with name a1.
%6 = "cir.get_bitfield"(%5) <{BitfieldInfo = #cir.bitfieldInfo<(name = "a1") (size 4) (storage !u64i 64) (offset 0) (signed = true)>}> : (!cir.ptr<!u64i>) -> !s32i
```
}];

let arguments = (ins
AnyType:$addr,
BitfieldInfoAttr:$BitfieldInfo
);

let results = (outs CIR_IntType:$result);

let builders = [
OpBuilder<(ins "Type":$type,
"Value":$addr,
"Type":$eltType,
"StringRef":$name,
"unsigned":$storageSize,
"unsigned":$size,
"unsigned":$offset,
"bool":$isSigned
),
[{
BitfieldInfoAttr info = BitfieldInfoAttr::get($_builder.getContext(), name, eltType, storageSize, size, offset, isSigned);
build($_builder, $_state, type, addr, info);
}]>
];
}

//===----------------------------------------------------------------------===//
// GetMemberOp
//===----------------------------------------------------------------------===//
Expand Down
Loading