Skip to content

Commit

Permalink
conv:tc: Format all files using project config.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkorenc committed Nov 2, 2023
1 parent 0e3524c commit a1c7494
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 192 deletions.
78 changes: 35 additions & 43 deletions include/vast/Conversion/TypeConverters/DataLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,34 @@ VAST_UNRELAX_WARNINGS

#include <array>

namespace vast::conv::tc
{
namespace vast::conv::tc {
// For each type remember its data layout information.
struct data_layout_blueprint
{
using dl_entry_interface = mlir::DataLayoutEntryInterface;
using dl_entry_attr = mlir::DataLayoutEntryAttr;
using dl_entry_attr = mlir::DataLayoutEntryAttr;

void add(mlir_type type, mlir_attr attr)
{
if (!attr)
void add(mlir_type type, mlir_attr attr) {
if (!attr) {
return;
}

auto it = entries.find(type);
if (it != entries.end())
{
VAST_CHECK(it->second == attr,
"New dl entry for type: {0} would make dl incosistent: {1} != {2}.",
type, it->second, attr);
if (it != entries.end()) {
VAST_CHECK(
it->second == attr,
"New dl entry for type: {0} would make dl incosistent: {1} != {2}.", type,
it->second, attr
);
return;
}

entries.try_emplace(type, attr);
}

mlir_attr wrap(mcontext_t &mctx) const
{
mlir_attr wrap(mcontext_t &mctx) const {
std::vector< dl_entry_interface > flattened;
for (const auto& [t, e] : entries)
{
for (const auto &[t, e] : entries) {
auto wrapped = dl_entry_attr::get(t, e);
VAST_ASSERT(wrapped);
flattened.push_back(wrapped);
Expand All @@ -67,24 +65,22 @@ namespace vast::conv::tc
// `make( ... )` can return empty attribute which means that the type should
// not be present in the new data layout.


// LLVM types should have `abi:pref` where `pref` is not required, but we
// are going to emit it anyways to make things easier.
// This whole functionality is 100% dependent on internals of `LLVM::` dialect
// that are not exposed anywhere and are probably free to change on version bumps.
struct llvm_dl_entry_helper
{
static mlir_attr make(mcontext_t &mctx, mlir_type llvm_type,
const dl::DLEntry &old_entry)
{
static mlir_attr
make(mcontext_t &mctx, mlir_type llvm_type, const dl::DLEntry &old_entry) {
// TODO(conv:tc): This has more complicated rules, consult `LLVM` dialect
// sources.
if (mlir::isa< mlir::LLVM::LLVMStructType >(llvm_type))
if (mlir::isa< mlir::LLVM::LLVMStructType >(llvm_type)) {
return {};
}

auto vector_type = mlir::VectorType::get({2}, mlir::IntegerType::get(&mctx, 32));
std::array< unsigned, 2 > entries =
{
auto vector_type = mlir::VectorType::get({ 2 }, mlir::IntegerType::get(&mctx, 32));
std::array< unsigned, 2 > entries = {
old_entry.abi_align,
// We currently do not have preferred option in `DLEntry`.
old_entry.abi_align
Expand All @@ -96,35 +92,33 @@ namespace vast::conv::tc

struct vast_dl_entry_helper
{
static mlir_attr make(mcontext_t &mctx, mlir_type vast_type,
const dl::DLEntry &old_entry)
{
static mlir_attr
make(mcontext_t &mctx, mlir_type vast_type, const dl::DLEntry &old_entry) {
auto new_entry = old_entry;
new_entry.type = vast_type;
return new_entry.create_raw_attr(mctx);
}
};

struct bultin_dl_entry_helper
struct builtin_dl_entry_helper
{
static mlir_attr make(mcontext_t &, mlir_type,
const dl::DLEntry &)
{
static mlir_attr make(mcontext_t &, mlir_type, const dl::DLEntry &) {
// Builtin types cannot be present in the data layout.
return {};
}
};

static inline mlir_attr make_entry(mlir_type trg_type, const dl::DLEntry &old_entry)
{
auto &mctx = *trg_type.getContext();
static inline mlir_attr make_entry(mlir_type trg_type, const dl::DLEntry &old_entry) {
auto &mctx = *trg_type.getContext();
auto trg_dialect = &trg_type.getDialect();

if (mlir::isa< mlir::BuiltinDialect >(trg_dialect))
return bultin_dl_entry_helper::make(mctx, trg_type, old_entry);
if (mlir::isa< mlir::BuiltinDialect >(trg_dialect)) {
return builtin_dl_entry_helper::make(mctx, trg_type, old_entry);
}

if (mlir::isa< mlir::LLVM::LLVMDialect >(trg_dialect))
if (mlir::isa< mlir::LLVM::LLVMDialect >(trg_dialect)) {
return llvm_dl_entry_helper::make(mctx, trg_type, old_entry);
}

// TODO(conv): Add helper function to check if dialect belongs
// to VAST suite and then assert on fallthrough?
Expand All @@ -133,18 +127,16 @@ namespace vast::conv::tc

// This is leaky abstraction of our data layout implementation, so maybe
// move this to `Util/DataLayout.hpp`?
static inline auto convert_data_layout_attrs(auto &type_converter)
{
return [&type_converter](mlir::DataLayoutSpecInterface spec)
{
static inline auto convert_data_layout_attrs(auto &type_converter) {
return [&type_converter](mlir::DataLayoutSpecInterface spec) {
data_layout_blueprint bp;
for (auto e : spec.getEntries())
{
for (auto e : spec.getEntries()) {
auto dl_entry = dl::DLEntry(e);
auto trg_type = type_converter.convert_type_to_type(dl_entry.type);
// What does this imply?
if (!trg_type)
if (!trg_type) {
continue;
}

bp.add(*trg_type, make_entry(*trg_type, std::move(dl_entry)));
}
Expand Down
69 changes: 26 additions & 43 deletions include/vast/Conversion/TypeConverters/HLToStd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ VAST_UNRELAX_WARNINGS
#include "vast/Dialect/HighLevel/HighLevelOps.hpp"
#include "vast/Dialect/HighLevel/HighLevelTypes.hpp"

#include "vast/Dialect/Core/CoreTypes.hpp"
#include "vast/Dialect/Core/CoreAttributes.hpp"
#include "vast/Dialect/Core/CoreTypes.hpp"

#include "vast/Conversion/Common/Types.hpp"
#include "vast/Conversion/TypeConverters/TypeConverter.hpp"
Expand All @@ -21,8 +21,7 @@ VAST_UNRELAX_WARNINGS
#include <algorithm>
#include <iostream>

namespace vast::conv::tc
{
namespace vast::conv::tc {
// TODO(conv:tc): Encode as concept.
// Requires of `self_t`
// * inherits/implements `mixins`
Expand All @@ -31,34 +30,29 @@ namespace vast::conv::tc
struct HLAggregates
{
private:
self_t &self() { return static_cast< self_t & >(*this); }
self_t &self() { return static_cast< self_t & >(*this); }

public:
// This is not a ctor so that users can position it as they want
// in their initialization.
void init()
{
void init() {
self().addConversion(convert_decayed_type());
self().addConversion(convert_lvalue_type());
self().addConversion(convert_pointer_type());
self().addConversion(convert_elaborated_type());
}

auto convert_decayed_type()
{
return [&](hl::DecayedType type)
{
auto convert_decayed_type() {
return [&](hl::DecayedType type) {
return Maybe(type.getElementType())
.and_then(self().convert_type_to_type())
.unwrap()
.template take_wrapped< maybe_type_t >();
};
}

auto convert_lvalue_type()
{
return [&](hl::LValueType type)
{
auto convert_lvalue_type() {
return [&](hl::LValueType type) {
return Maybe(type.getElementType())
.and_then(self().convert_type_to_type())
.unwrap()
Expand All @@ -67,10 +61,8 @@ namespace vast::conv::tc
};
}

auto convert_elaborated_type()
{
return [&](hl::ElaboratedType type)
{
auto convert_elaborated_type() {
return [&](hl::ElaboratedType type) {
using raw = hl::ElaboratedType;

return Maybe(type.getElementType())
Expand All @@ -81,10 +73,8 @@ namespace vast::conv::tc
};
}

auto convert_pointer_type()
{
return [&](hl::PointerType type)
{
auto convert_pointer_type() {
return [&](hl::PointerType type) {
using raw = hl::PointerType;

return Maybe(type.getElementType())
Expand All @@ -94,14 +84,11 @@ namespace vast::conv::tc
.template take_wrapped< maybe_type_t >();
};
}
protected:

auto convert_pointer_element_type()
{
return [&](auto t) -> maybe_type_t
{
if (t.template isa< hl::VoidType >())
{
protected:
auto convert_pointer_element_type() {
return [&](auto t) -> maybe_type_t {
if (t.template isa< hl::VoidType >()) {
auto sign = mlir::IntegerType::SignednessSemantics::Signless;
return self().int_type(8u, sign);
}
Expand All @@ -112,27 +99,22 @@ namespace vast::conv::tc
// `args` is passed by value as I have no idea how to convince clang
// to not pass in `quals` as `const` when forwarding.
// They are expected to be lightweight objects anyway.
template< typename T, typename ... Args >
auto make_aggregate_type(Args ... args)
{
return [=](mlir_type elem)
{
return T::get(elem.getContext(), elem, args ...);
};
template< typename T, typename... Args >
auto make_aggregate_type(Args... args) {
return [=](mlir_type elem) { return T::get(elem.getContext(), elem, args...); };
}

};

struct HLToStd : base_type_converter
, mixins< HLToStd >
, HLAggregates< HLToStd >
struct HLToStd
: base_type_converter
, mixins< HLToStd >
, HLAggregates< HLToStd >
{
const mlir::DataLayout &dl;
mlir::MLIRContext &mctx;

HLToStd(const mlir::DataLayout &dl, mcontext_t &mctx)
: base_type_converter(), dl(dl), mctx(mctx)
{
: base_type_converter(), dl(dl), mctx(mctx) {
// Fallthrough option - we define it first as it seems the framework
// goes from the last added conversion.
addConversion([&](mlir_type t) -> maybe_type_t {
Expand Down Expand Up @@ -163,8 +145,9 @@ namespace vast::conv::tc
// TODO(lukas): Take optional to denote that is may be `Signless`.
auto int_type(unsigned bitwidth, bool is_signed) {
auto signedness = [=]() {
if (is_signed)
if (is_signed) {
return mlir::IntegerType::SignednessSemantics::Signed;
}
return mlir::IntegerType::SignednessSemantics::Unsigned;
}();
return mlir::IntegerType::get(&this->mctx, bitwidth, signedness);
Expand Down
Loading

0 comments on commit a1c7494

Please sign in to comment.