-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlir_demo.zig
More file actions
65 lines (53 loc) · 2.16 KB
/
mlir_demo.zig
File metadata and controls
65 lines (53 loc) · 2.16 KB
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
const std = @import("std");
const lib = @import("ora_lib");
const c = @cImport({
@cInclude("mlir-c/IR.h");
@cInclude("mlir-c/Support.h");
@cInclude("mlir-c/RegisterEverything.h");
});
fn writeToFile(str: c.MlirStringRef, user_data: ?*anyopaque) callconv(.C) void {
const file: *std.fs.File = @ptrCast(@alignCast(user_data.?));
_ = file.writeAll(str.data[0..str.length]) catch {};
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len < 2) {
std.debug.print("Usage: mlir_demo <input.ora> [output.mlir]\n", .{});
return;
}
const input = args[1];
const output = if (args.len >= 3) args[2] else "output.mlir";
// Frontend: lex + parse to AST
const source = try std.fs.cwd().readFileAlloc(allocator, input, 10 * 1024 * 1024);
defer allocator.free(source);
var lexer = lib.Lexer.init(allocator, source);
defer lexer.deinit();
const tokens = try lexer.scanTokens();
defer allocator.free(tokens);
var arena = lib.ast_arena.AstArena.init(allocator);
defer arena.deinit();
var parser = lib.Parser.init(tokens, &arena);
parser.setFileId(1);
const ast_nodes = try parser.parse();
_ = ast_nodes; // Placeholder: real lowering would traverse AST
// MLIR: create empty module and print to file
const ctx = c.mlirContextCreate();
defer c.mlirContextDestroy(ctx);
const registry = c.mlirDialectRegistryCreate();
defer c.mlirDialectRegistryDestroy(registry);
c.mlirRegisterAllDialects(registry);
c.mlirContextAppendDialectRegistry(ctx, registry);
c.mlirContextLoadAllAvailableDialects(ctx);
const loc = c.mlirLocationUnknownGet(ctx);
const module = c.mlirModuleCreateEmpty(loc);
defer c.mlirModuleDestroy(module);
var file = try std.fs.cwd().createFile(output, .{});
defer file.close();
const op = c.mlirModuleGetOperation(module);
c.mlirOperationPrint(op, writeToFile, @ptrCast(&file));
std.debug.print("Wrote MLIR to {s}\n", .{output});
}