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 1 commit
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, get_value, Location, Block, Region, Attribute, create_operation, context, IndexType
jumerckx marked this conversation as resolved.
Show resolved Hide resolved
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, get_value, Location, Block, Region, Attribute, create_operation, context, IndexType
jumerckx marked this conversation as resolved.
Show resolved Hide resolved
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
Collaborator

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"(({0} != nothing) && push!(operands, get_value{2}({0}){1})
jumerckx marked this conversation as resolved.
Show resolved Hide resolved
)",
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"(get_value{0}({1}){2}, )", (variadic ? "." : ""), operandname, (variadic ? "..." : ""));
jumerckx marked this conversation as resolved.
Show resolved Hide resolved
separator = (!alreadykeyword && i == op.getNumOperands() - 1) ? "; " : ", ";
}

operandarguments += operandname + defaultvalue + "::" + type + separator;
operandarguments += operandname + defaultvalue + separator;
}
if (operandarguments == "") {
operandarguments = "; ";
Expand Down
11 changes: 11 additions & 0 deletions src/IR/Value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ 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))


jumerckx marked this conversation as resolved.
Show resolved Hide resolved
abstract type MLIRValueTrait end
struct Convertible <: MLIRValueTrait end
struct NonConvertible <: MLIRValueTrait end

MLIRValueTrait(T) = NonConvertible()
get_value(x::Value) = x
get_value(x::T) where T = get_value(MLIRValueTrait(T), x)
get_value(::Convertible, x) = x.value
get_value(::NonConvertible, x::T) where T = error("Type $T does not have the Convertible MLIRValueTrait")
jumerckx marked this conversation as resolved.
Show resolved Hide resolved

"""
==(value1, value2)

Expand Down
Loading