Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MLIRValueTrait to support user defined Value types #64

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
22 changes: 7 additions & 15 deletions deps/tblgen/jl-generators.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ bool emitOpTableDefs(const llvm::RecordKeeper &recordKeeper,
const char *moduleTemplate;
if (disableModuleWrap)
{
moduleTemplate = R"(import ...IR: IR, NamedAttribute, Value, Location, Block, Region, Attribute, create_operation, context, IndexType
moduleTemplate = R"(import ...IR: IR, NamedAttribute, value, Location, Block, Region, Attribute, create_operation, context, IndexType
import ..Dialects: namedattribute, operandsegmentsizes
import ...API

Expand All @@ -181,7 +181,7 @@ import ...API
{
moduleTemplate = R"(module {0}

import ...IR: NamedAttribute, Value, Location, Block, Region, Attribute, create_operation, context, IndexType
import ...IR: NamedAttribute, value, Location, Block, Region, Attribute, create_operation, context, IndexType
import ..Dialects: namedattribute, operandsegmentsizes
import ...API

Expand All @@ -197,7 +197,7 @@ function {0}({1}location=Location())
end
)"; // 0: functionname, 1: functionarguments, 2: functionbody
const char *functionbodytemplate = R"(results = IR.Type[{0}]
operands = Value[{1}]
Copy link
Member

Choose a reason for hiding this comment

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

Why use API.MlirValue instead of IR.Value?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right, I don't it should.

operands = API.MlirValue[{1}]
owned_regions = Region[{2}]
successors = Block[{3}]
attributes = NamedAttribute[{4}]
Expand Down Expand Up @@ -250,23 +250,15 @@ end
}
operandname = sanitizeName(operandname);

std::string type = "Value";

bool optional = named_operand.isOptional();
bool variadic = named_operand.isVariadic();

if (variadic)
{
type = "Vector{" + type + "}";
}

std::string separator = ", ";
if (optional)
{
optionals += llvm::formatv(R"(!isnothing({0}) && push!(operands, {0}{1})
optionals += llvm::formatv(R"(!isnothing({0}) && push!(operands, value{2}({0}){1})
)",
operandname, (variadic ? "..." : ""));
type = "Union{Nothing, " + type + "}";
operandname, (variadic ? "..." : ""), (variadic ? "." : ""));
defaultvalue = "=nothing";

if (!alreadykeyword) {
Expand All @@ -276,11 +268,11 @@ end
}
else
{
operandcontainer += operandname + (variadic ? "..." : "") + ", ";
operandcontainer += llvm::formatv(R"(value{0}({1}){2}, )", (variadic ? "." : ""), operandname, (variadic ? "..." : ""));
separator = (!alreadykeyword && i == op.getNumOperands() - 1) ? "; " : ", ";
}

operandarguments += operandname + defaultvalue + "::" + type + separator;
operandarguments += operandname + defaultvalue + separator;
}
if (operandarguments == "") {
operandarguments = "; ";
Expand Down
10 changes: 10 additions & 0 deletions src/IR/Value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ Base.convert(::Core.Type{API.MlirValue}, value::Value) = value.value
Base.size(value::Value) = Base.size(Type(value))
Base.ndims(value::Value) = Base.ndims(Type(value))

abstract type ValueTrait end
struct Convertible <: ValueTrait end
struct NonConvertible <: ValueTrait end

ValueTrait(T) = NonConvertible()
value(x::Value) = x
value(x::T) where T = value(ValueTrait(T), x)
value(::Convertible, x) = x.value
value(::NonConvertible, x::T) where T = error("Type $T does not have the Convertible ValueTrait")

"""
==(value1, value2)

Expand Down
Loading