From d7fb2121a569935dcb333fefce53b8a3be27c5a1 Mon Sep 17 00:00:00 2001 From: z277zhu Date: Wed, 15 Nov 2023 14:06:55 -0500 Subject: [PATCH] Added patch to llvm & works on loading mir files --- WORKSPACE | 2 ++ gematria/datasets/bhive_importer_test.cc | 5 +++++ gematria/datasets/python/BUILD | 11 +++++++++++ gematria/datasets/python/import_from_mir.py | 6 ++++-- mir_parser.patch | 22 +++++++++++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 mir_parser.patch diff --git a/WORKSPACE b/WORKSPACE index ba9a820e..75dbf335 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -175,6 +175,8 @@ http_archive( sha256 = LLVM_SHA256, strip_prefix = "llvm-project-" + LLVM_COMMIT, urls = ["https://github.com/llvm/llvm-project/archive/{commit}.zip".format(commit = LLVM_COMMIT)], + patches = ["//:mir_parser.patch"], # Hack to make the MIR parser work + patch_args = ["-p1"], ) load( diff --git a/gematria/datasets/bhive_importer_test.cc b/gematria/datasets/bhive_importer_test.cc index 1094aefe..580debf8 100644 --- a/gematria/datasets/bhive_importer_test.cc +++ b/gematria/datasets/bhive_importer_test.cc @@ -234,5 +234,10 @@ TEST_F(BHiveImporterTest, MIRDatasetBasicTest) { IsOk()); } +TEST_F(BHiveImporterTest, MIRDatasetTest2) { + EXPECT_THAT(x86_bhive_importer_->LoadMIRModule("/u9/z277zhu/research/gematria/mir_input/output/native_test.mir"), + IsOk()); +} + } // namespace } // namespace gematria diff --git a/gematria/datasets/python/BUILD b/gematria/datasets/python/BUILD index 0271cc55..fca892c7 100644 --- a/gematria/datasets/python/BUILD +++ b/gematria/datasets/python/BUILD @@ -53,3 +53,14 @@ gematria_py_binary( "//gematria/utils/python:pybind11_abseil_status", ], ) + +gematria_py_binary( + name = "import_from_mir", + srcs = ["import_from_mir.py"], + deps = [ + ":bhive_importer", + "//gematria/llvm/python:canonicalizer", + "//gematria/llvm/python:llvm_architecture_support", + "//gematria/utils/python:pybind11_abseil_status", + ], +) diff --git a/gematria/datasets/python/import_from_mir.py b/gematria/datasets/python/import_from_mir.py index 460abece..d76e9ee5 100644 --- a/gematria/datasets/python/import_from_mir.py +++ b/gematria/datasets/python/import_from_mir.py @@ -126,9 +126,11 @@ def main(argv: Sequence[str]) -> None: for filename in os.listdir(_INPUT_DIR.value): if filename.endswith(".mir"): mir_file = os.path.join(_INPUT_DIR.value, filename) - perf_file = os.path.join(_INPUT_DIR.value, filename.replace(".mir", ".perf")) + print("mir file is " + mir_file) + perf_file = os.path.join(_INPUT_DIR.value, filename.replace(".mir", ".ll.perf")) # load the MIR file - module = importer.loadMIRModule(mir_file) + module = importer.LoadMIRModule(mir_file) + logging.info('Procssing %s file', mir_file) # iterate over each line in the corresponding .perf file with tf.io.gfile.GFile(perf_file, 'r') as bhive_csv_file: for line in bhive_csv_file: diff --git a/mir_parser.patch b/mir_parser.patch new file mode 100644 index 00000000..c8bc02e2 --- /dev/null +++ b/mir_parser.patch @@ -0,0 +1,22 @@ +diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp +index a1dde3e1fecb..88d8b11180e1 100644 +--- a/llvm/lib/MC/MCContext.cpp ++++ b/llvm/lib/MC/MCContext.cpp +@@ -201,8 +201,15 @@ MCInst *MCContext::createMCInst() { + MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) { + SmallString<128> NameSV; + StringRef NameRef = Name.toStringRef(NameSV); +- +- assert(!NameRef.empty() && "Normal symbols cannot be unnamed!"); ++ static int cnt = 0; ++ if (NameRef.empty()){ ++ // Append cnt to the end of NameRef ++ std::string ModifiedName = ("INVALID" + std::to_string(cnt)); ++ cnt++; // Increment cnt ++ ++ // Convert ModifiedName back to StringRef ++ NameRef = StringRef(ModifiedName); ++ } + + MCSymbol *&Sym = Symbols[NameRef]; + if (!Sym)