MLLIF
a MLIR-based Language to Language Interoperability Flyover
All Classes Namespaces Files Functions Variables Typedefs Properties Macros Pages
CIRType.cxx
Go to the documentation of this file.
1/*
2 * Copyright 2025 Yeong-won Seo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "pch.h"
18
21
22namespace {
23 std::deque<std::string> Split(std::string s, const std::string &del) {
24 size_t start = 0, end;
25 const size_t del_len = del.length();
26 std::deque<std::string> res;
27
28 while ((end = s.find(del, start)) != std::string::npos) {
29 auto token = s.substr(start, end - start);
30 res.push_back(token);
31
32 start = end + del_len;
33 }
34
35 res.push_back(s.substr(start));
36 return res;
37 }
38} // namespace
39
40auto mllif::mlir::cir::CIRType::From(const ::mlir::Type &type, const std::shared_ptr<::mlir::ModuleOp> &module) -> std::shared_ptr<CIRType> {
41 if (dyn_cast<::cir::BoolType>(type)) {
42 return std::make_shared<CIRBoolType>();
43 }
44
45 if (const auto i = dyn_cast<::cir::IntType>(type)) {
46 return std::make_shared<CIRIntegerType>(
47 i.getWidth(),
48 !type.isUnsignedInteger() // Regard signless as signed
49 );
50 }
51
52 if (dyn_cast<::cir::FP16Type>(type)) {
53 return std::make_shared<CIRFloatType>(16);
54 }
55 if (dyn_cast<::cir::SingleType>(type)) {
56 return std::make_shared<CIRFloatType>(32);
57 }
58 if (dyn_cast<::cir::DoubleType>(type)) {
59 return std::make_shared<CIRFloatType>(64);
60 }
61 if (dyn_cast<::cir::FP128Type>(type)) {
62 return std::make_shared<CIRFloatType>(128);
63 }
64
65 if (const auto pointer = dyn_cast<::cir::PointerType>(type)) {
66 return std::make_shared<CIRPointerType>(From(pointer.getPointee(), module));
67 }
68
69 if (const auto _struct = dyn_cast<::cir::RecordType>(type)) {
70 const ::mlir::DataLayout layout{module->clone()};
71 const auto params = module->getDataLayoutSpec().getEntries();
72
73 const auto size = _struct.getTypeSize(layout, module->getDataLayoutSpec().getEntries());
74 const auto align = _struct.getABIAlignment(layout, params);
75
76 const auto path = Split(_struct.getName().str(), "::");
77
78 return std::make_shared<CIRStructType>(path, size, align);
79 }
80
81 return nullptr;
82}
83
84auto mllif::mlir::cir::CIRBoolType::store(Tree &/**/) const -> std::string {
85 return "bool";
86}
87
88auto mllif::mlir::cir::CIRIntegerType::store(Tree &/**/) const -> std::string {
89 std::stringstream ss;
90 ss << (signed_() ? 's' : 'u') << width();
91 return ss.str();
92}
93
94auto mllif::mlir::cir::CIRFloatType::store(Tree &/**/) const -> std::string {
95 std::stringstream ss;
96 ss << "fp" << width();
97 return ss.str();
98}
99
100auto mllif::mlir::cir::CIRPointerType::store(Tree &symbols) const -> std::string {
101 std::stringstream ss;
102 ss << pointee()->store(symbols) << '*';
103 return ss.str();
104}
105
106auto mllif::mlir::cir::CIRStructType::store(Tree &symbols) const -> std::string {
107 auto copyPath = path();
108 const auto symbol = symbols.root().insert_inplace(copyPath, shared::type::Object);
109
110 bool sizeSet = false, alignSet = false;
111 for (auto &[key, value] : symbol->attributes()) {
112 if (key == "size") {
113 sizeSet = true;
114 } else if (key == "align") {
115 alignSet = true;
116 }
117 }
118 if (!sizeSet) {
119 symbol->attributes().emplace_back(std::make_pair("size", std::to_string(size())));
120 }
121 if (!alignSet) {
122 symbol->attributes().emplace_back(std::make_pair("align", std::to_string(align())));
123 }
124
125 std::stringstream ss;
126 for (const auto &term : path()) {
127 ss << '/' << term;
128 }
129 return ss.str();
130}
A tree struct for symbol tree. It's just a simple wrapper for root node.
Definition Tree.h:147
std::string store(Tree &symbols) const override
Stores this type into symbol tree if necessary and Returns reference string for this type.
Definition CIRType.cxx:84
std::string store(Tree &symbols) const override
Stores this type into symbol tree if necessary and Returns reference string for this type.
Definition CIRType.cxx:94
std::string store(Tree &symbols) const override
Stores this type into symbol tree if necessary and Returns reference string for this type.
Definition CIRType.cxx:88
std::string store(Tree &symbols) const override
Stores this type into symbol tree if necessary and Returns reference string for this type.
Definition CIRType.cxx:100
std::shared_ptr< CIRType > & pointee()
Definition CIRType.h:92
std::string store(Tree &symbols) const override
Stores this type into symbol tree if necessary and Returns reference string for this type.
Definition CIRType.cxx:106
std::deque< std::string > & path()
Definition CIRType.h:109
static auto From(const ::mlir::Type &type, const std::shared_ptr<::mlir::ModuleOp > &module) -> std::shared_ptr< CIRType >
Definition CIRType.cxx:40
constexpr std::string Object
Definition annotation.h:37