From 61b79096bcff3250b86523b0de57ee5d1f3ac6b8 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Mon, 6 Jan 2025 20:26:29 +0900 Subject: [PATCH 1/3] Refactor --- macro/src/dialect.rs | 47 +++++++++++++++----------- macro/src/dialect/input.rs | 23 +++++++++++++ macro/src/dialect/input/input_field.rs | 14 ++++++++ 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/macro/src/dialect.rs b/macro/src/dialect.rs index a87054c74e..162b0a5357 100644 --- a/macro/src/dialect.rs +++ b/macro/src/dialect.rs @@ -16,27 +16,20 @@ use operation::Operation; use proc_macro::TokenStream; use proc_macro2::Span; use quote::quote; -use std::{env, fmt::Display, path::Path, str}; +use std::{ + env, + fmt::Display, + path::{Component, Path}, + str, +}; use tblgen::{record::Record, record_keeper::RecordKeeper, TableGenParser}; +const LLVM_INCLUDE_DIRECTORY: &str = env!("LLVM_INCLUDE_DIRECTORY"); + pub fn generate_dialect(input: DialectInput) -> Result> { let mut parser = TableGenParser::new(); if let Some(source) = input.table_gen() { - let base = Path::new(env!("LLVM_INCLUDE_DIRECTORY")); - - // Here source looks like `include "foo.td" include "bar.td"`. - for (index, path) in source.split_ascii_whitespace().enumerate() { - if index % 2 == 0 { - continue; // skip "include" - } - - let path = &path[1..(path.len() - 1)]; // remove "" - let path = Path::new(path).parent().unwrap(); - let path = base.join(path); - parser = parser.add_include_path(&path.to_string_lossy()); - } - parser = parser.add_source(source).map_err(create_syn_error)?; } @@ -44,13 +37,29 @@ pub fn generate_dialect(input: DialectInput) -> Result, + // TODO Remove this field. td_file: Option, + // TODO Remove this field. include_directories: Vec, + files: Vec, + directories: Vec, } impl DialectInput { @@ -27,6 +32,14 @@ impl DialectInput { pub fn include_directories(&self) -> impl Iterator { self.include_directories.iter().map(Deref::deref) } + + pub fn files(&self) -> impl Iterator { + self.files.iter().map(Deref::deref) + } + + pub fn directories(&self) -> impl Iterator { + self.directories.iter().map(Deref::deref) + } } impl Parse for DialectInput { @@ -35,6 +48,8 @@ impl Parse for DialectInput { let mut table_gen = None; let mut td_file = None; let mut includes = vec![]; + let mut files = vec![]; + let mut directories = vec![]; for item in Punctuated::::parse_terminated(input)? { match item { @@ -44,6 +59,12 @@ impl Parse for DialectInput { InputField::IncludeDirectories(field) => { includes = field.into_iter().map(|literal| literal.value()).collect() } + InputField::Files(field) => { + files = field.into_iter().map(|literal| literal.value()).collect() + } + InputField::Directories(field) => { + directories = field.into_iter().map(|literal| literal.value()).collect() + } } } @@ -52,6 +73,8 @@ impl Parse for DialectInput { table_gen, td_file, include_directories: includes, + files, + directories, }) } } diff --git a/macro/src/dialect/input/input_field.rs b/macro/src/dialect/input/input_field.rs index 61a161fca6..e6c8fad5e4 100644 --- a/macro/src/dialect/input/input_field.rs +++ b/macro/src/dialect/input/input_field.rs @@ -7,6 +7,8 @@ pub enum InputField { TableGen(LitStr), TdFile(LitStr), IncludeDirectories(Punctuated), + Files(Punctuated), + Directories(Punctuated), } impl Parse for InputField { @@ -27,6 +29,18 @@ impl Parse for InputField { Ok(Self::IncludeDirectories( Punctuated::::parse_terminated(&content)?, )) + } else if ident == format_ident!("files") { + let content; + bracketed!(content in input); + Ok(Self::Files( + Punctuated::::parse_terminated(&content)?, + )) + } else if ident == format_ident!("include_directories") { + let content; + bracketed!(content in input); + Ok(Self::Directories( + Punctuated::::parse_terminated(&content)?, + )) } else { Err(input.error(format!("invalid field {}", ident))) } From 206cf1967af38a70ff6e7b7dbd2b427a7dfb92ad Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Mon, 6 Jan 2025 20:29:55 +0900 Subject: [PATCH 2/3] Fix include macro uses --- melior/src/dialect/ods.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/melior/src/dialect/ods.rs b/melior/src/dialect/ods.rs index 5591aeab89..b04d5505e4 100644 --- a/melior/src/dialect/ods.rs +++ b/melior/src/dialect/ods.rs @@ -32,9 +32,8 @@ melior_macro::dialect! { } melior_macro::dialect! { name: "arm_sme", - table_gen: r#"include "mlir/Dialect/ArmSME/IR/ArmSME.td" - include "mlir/Dialect/ArmSME/IR/ArmSMEOps.td" - include "mlir/Dialect/ArmSME/IR/ArmSMEIntrinsicOps.td""# + files: ["ArmSME.td", "ArmSMEOps.td", "ArmSMEIntrinsicOps.td"], + include_directories: ["mlir/Dialect/ArmSME/IR"], } melior_macro::dialect! { name: "async", @@ -86,7 +85,8 @@ melior_macro::dialect! { } melior_macro::dialect! { name: "irdl", - table_gen: r#"include "mlir/Dialect/IRDL/IR/IRDL.td" include "mlir/Dialect/IRDL/IR/IRDLOps.td""# + files: ["IRDL.td", "IRDLOps.td"], + include_directories: ["mlir/Dialect/IRDL/IR"], } melior_macro::dialect! { name: "llvm", From e6323a043d0334c8be5c08b55a6c4e56f5541461 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Mon, 6 Jan 2025 20:57:27 +0900 Subject: [PATCH 3/3] Fix --- Cargo.lock | 3 +-- macro/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d2204c7de1..ba7616e071 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -566,8 +566,7 @@ dependencies = [ [[package]] name = "tblgen-alt" version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ecbc9175dd38627cd01d546e7b41c9a115e5773f4c98f64e2185c81ec5f45ab" +source = "git+https://github.com/raviqqe/tblgen-rs-alt?branch=feature%2Finclude-file#0e0824a9f0fb10192d389599a772dfc60e4c5d26" dependencies = [ "bindgen 0.69.4", "cc", diff --git a/macro/Cargo.toml b/macro/Cargo.toml index 4a6da71c2d..9e5a4da79d 100644 --- a/macro/Cargo.toml +++ b/macro/Cargo.toml @@ -19,7 +19,7 @@ proc-macro2 = "1" quote = "1" regex = "1.11.1" syn = { version = "2", features = ["full"] } -tblgen = { version = "0.4.0", features = [ +tblgen = { git = "https://github.com/raviqqe/tblgen-rs-alt", branch = "feature/include-file", features = [ "llvm19-0", ], default-features = false, package = "tblgen-alt" } unindent = "0.2.3"