-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path00_jlir_dialect.jl
137 lines (107 loc) · 3.8 KB
/
00_jlir_dialect.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# using Preferences
# set_preferences!(
# MLIR.API.MLIR_jll,
# "mlir_c_path" => "/home/jumerckx/julia/build/debug/standalone/lib/libStandaloneCAPITestLib.so",
# )
using MLIR
ctx = MLIR.API.mlirContextCreate()
function registerAllDialects!(ctx)
registry = MLIR.API.mlirDialectRegistryCreate()
MLIR.API.mlirRegisterAllDialects(registry)
handle = MLIR.API.mlirGetDialectHandle__jlir__()
MLIR.API.mlirDialectHandleRegisterDialect(handle, ctx)
MLIR.API.mlirContextAppendDialectRegistry(ctx, registry)
MLIR.API.mlirDialectRegistryDestroy(registry)
return nothing
end
registerAllDialects!(ctx)
ir = """
pdl.pattern : benefit(1) {
%root = pdl.operation "test.op"
pdl.rewrite %root {
pdl.operation "test.success2"
pdl.erase %root
}
}
"""
mod = MLIR.API.mlirModuleCreateParse(ctx, ir)
op = MLIR.API.mlirModuleGetOperation(mod)
MLIR.API.mlirOperationVerify(op)
ir = """
module {
func.func nested @"Tuple{typeof(Main.branches), Bool}"(%arg0: !jlir<"typeof(Main.branches)">, %arg1: !jlir.Bool) -> !jlir.Bool attributes {llvm.emit_c_interface} {
"jlir.goto"()[^bb1] : () -> ()
^bb1: // pred: ^bb0
"jlir.gotoifnot"(%arg1)[^bb3, ^bb2] {operand_segment_sizes = dense<[1, 0, 0]> : vector<3xi32>} : (!jlir.Bool) -> ()
^bb2: // pred: ^bb1
%0 = "jlir.pi"(%arg1) : (!jlir.Bool) -> !jlir.Bool
"jlir.return"(%0) : (!jlir.Bool) -> ()
^bb3: // pred: ^bb1
%1 = "jlir.constant"() {value = #jlir.true} : () -> !jlir.Bool
"jlir.return"(%1) : (!jlir.Bool) -> ()
}
}
"""
ir = """
module {
func.func nested @"Tuple{jlir.Function, Bool}"(%arg0: !jlir.Bool, %arg1: !jlir.Bool) -> !jlir.Bool attributes {llvm.emit_c_interface} {
"jlir.goto"()[^bb1] : () -> ()
^bb1: // pred: ^bb0
"jlir.gotoifnot"(%arg1)[^bb3, ^bb2] {operand_segment_sizes = dense<[1, 0, 0]> : vector<3xi32>} : (!jlir.Bool) -> ()
^bb2: // pred: ^bb1
%0 = "jlir.pi"(%arg1) : (!jlir.Bool) -> !jlir.Bool
"jlir.return"(%0) : (!jlir.Bool) -> ()
^bb3: // pred: ^bb1
%1 = "jlir.constant"() {value = #jlir.true} : () -> !jlir.Bool
"jlir.return"(%1) : (!jlir.Bool) -> ()
}
}
"""
mod = MLIR.API.mlirModuleCreateParse(ctx, ir)
function lowerModuleToLLVM(ctx, mod)
pm = MLIR.API.mlirPassManagerCreate(ctx)
op = "func.func"
opm = MLIR.API.mlirPassManagerGetNestedUnder(pm, op)
MLIR.API.mlirPassManagerAddOwnedPass(pm,
MLIR.API.mlirCreateConversionConvertFuncToLLVM()
)
MLIR.API.mlirOpPassManagerAddOwnedPass(opm,
MLIR.API.mlirCreateConversionConvertArithmeticToLLVM()
)
status = MLIR.API.mlirPassManagerRun(pm, mod)
# undefined symbol: mlirLogicalResultIsFailure
if status.value == 0
error("Unexpected failure running pass failure")
end
MLIR.API.mlirPassManagerDestroy(pm)
end
lowerModuleToLLVM(ctx, mod)
using MLIR: IR
ctx = IR.Context()
IR.get_or_load_dialect!(ctx, "jlir") |> typeof
op = IR.create_operation("jlir.neg_int", IR.Location(ctx));
typeof(op)
op = IR.create_operation("jlir.pi", IR.Location(ctx), results=[typeof(pi)]);
op = IR.create_operation("jlir.checked_sadd_int", IR.Location(ctx), results=[typeof(Int)]);
typeof(op)
op = IR.create_operation("jlir.add_int", IR.Location(ctx));
typeof(op)
using MLIR: IR
ctx = IR.Context()
IR.get_or_load_dialect!(ctx, "jlir");
loc = IR.Location(ctx)
op1 = IR.create_operation("jlir.add_int", loc ); # OK
op2 = IR.create_operation("jlir.add_int", loc, results = [Int] ); # OK
op3 = IR.create_operation("jlir.add_int", loc, results = [String] ); # OK?
op3 = IR.create_operation("jlir.add_int", loc, operands = [Int, Int] ); # crash
include("./Utils.jl")
using .Utils
function pow(x::F, n::Integer) where {F}
p = one(F)
for _ in 1:n
p *= x
end
p
end
ir, ret = @code_ircode pow(2, 10)
println(@dot pow(2, 10))