Skip to content

Commit 5b641fd

Browse files
committed
merge main to the branch
1 parent 9785d0a commit 5b641fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2103
-208
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
From c4d93d8def7a5b77ba3d8ee41e5bb8f4259f7b1c Mon Sep 17 00:00:00 2001
2+
From: Christopher Bate <cbate@nvidia.com>
3+
Date: Mon, 5 Jan 2026 23:34:56 +0000
4+
Subject: [PATCH 1/1] Solve multiple minor bugs in StablehloRefineShapes and
5+
StablehloAggressiveFolder
6+
7+
---
8+
.../conversions/linalg/transforms/MapStablehloToScalarOp.h | 5 +++++
9+
stablehlo/transforms/StablehloRefineShapes.cpp | 3 ++-
10+
.../transforms/optimization/StablehloAggressiveFolder.cpp | 3 ++-
11+
3 files changed, 9 insertions(+), 2 deletions(-)
12+
13+
diff --git a/stablehlo/conversions/linalg/transforms/MapStablehloToScalarOp.h b/stablehlo/conversions/linalg/transforms/MapStablehloToScalarOp.h
14+
index 2cf82769..9634bbda 100644
15+
--- a/stablehlo/conversions/linalg/transforms/MapStablehloToScalarOp.h
16+
+++ b/stablehlo/conversions/linalg/transforms/MapStablehloToScalarOp.h
17+
@@ -164,6 +164,7 @@ struct StablehloToScalarOp<stablehlo::TanOp> {
18+
template <>
19+
struct StablehloToScalarOp<stablehlo::Atan2Op> {
20+
using COp = ::mlir::complex::Atan2Op;
21+
+ using FOp = ::mlir::math::Atan2Op;
22+
};
23+
24+
template <>
25+
@@ -831,6 +832,10 @@ inline Value mapStablehloOpToStdScalarOp<stablehlo::BitcastConvertOp>(
26+
Type argType = getElementTypeOrSelf(argTypes.front());
27+
Type resultType = getElementTypeOrSelf(resultTypes.front());
28+
29+
+ // arith::BitcastOp doesn't support complex types, so bail out if either type
30+
+ // is complex.
31+
+ if (!argType.isIntOrFloat() || !resultType.isIntOrFloat()) return nullptr;
32+
+
33+
if (resultType.getIntOrFloatBitWidth() != argType.getIntOrFloatBitWidth())
34+
return nullptr;
35+
36+
diff --git a/stablehlo/transforms/StablehloRefineShapes.cpp b/stablehlo/transforms/StablehloRefineShapes.cpp
37+
index 4b585b46..80fd237b 100644
38+
--- a/stablehlo/transforms/StablehloRefineShapes.cpp
39+
+++ b/stablehlo/transforms/StablehloRefineShapes.cpp
40+
@@ -45,6 +45,7 @@ limitations under the License.
41+
#include "mlir/IR/Value.h"
42+
#include "mlir/IR/ValueRange.h"
43+
#include "mlir/Interfaces/InferTypeOpInterface.h"
44+
+#include "mlir/Interfaces/SideEffectInterfaces.h"
45+
#include "mlir/Rewrite/FrozenRewritePatternSet.h"
46+
#include "mlir/Support/LLVM.h"
47+
#include "mlir/Support/LogicalResult.h"
48+
@@ -565,7 +566,7 @@ struct RefineCallOpPattern : public OpRewritePattern<func::CallOp> {
49+
auto callee = refinementKey->getFunc();
50+
std::optional<SmallVector<DenseIntElementsAttr>> constantAttrs =
51+
isConstantFunction(callee);
52+
- if (constantAttrs.has_value()) {
53+
+ if (constantAttrs.has_value() && mlir::isMemoryEffectFree(callee)) {
54+
SmallVector<Value> constants;
55+
for (auto constAttr : constantAttrs.value()) {
56+
constants.push_back(
57+
diff --git a/stablehlo/transforms/optimization/StablehloAggressiveFolder.cpp b/stablehlo/transforms/optimization/StablehloAggressiveFolder.cpp
58+
index 8dac8c97..c10f2ae7 100644
59+
--- a/stablehlo/transforms/optimization/StablehloAggressiveFolder.cpp
60+
+++ b/stablehlo/transforms/optimization/StablehloAggressiveFolder.cpp
61+
@@ -625,7 +625,8 @@ struct FoldCompareOpPattern : public ShapeOpRewritePattern<CompareOp> {
62+
LogicalResult matchAndRewrite(CompareOp op,
63+
PatternRewriter& rewriter) const override {
64+
auto resultType = op.getType();
65+
- if (failed(validateShapeFoldDtype(rewriter, op, resultType)))
66+
+ if (failed(validateStaticShapeResult(rewriter, op, resultType)) ||
67+
+ failed(validateShapeFoldDtype(rewriter, op, resultType)))
68+
return failure();
69+
70+
auto res = foldBinaryOpIntOrFloat<FoldCompare, IntegerAttr, IntegerAttr>(
71+
--
72+
2.52.0
73+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# GenerateEmbeddedStandaloneCPP.cmake
2+
#
3+
# CMake script that generates a C++ source file embedding a list of text files
4+
# as raw string literals. Intended to embed the StandaloneCPP runtime sources
5+
# into the compiler so that support files can be emitted without reading the
6+
# source tree at runtime.
7+
#
8+
# Expected variables:
9+
# - OUTPUT: output .cpp file path
10+
# - INPUT_FILES: CMake list of absolute input file paths
11+
# - REL_PATHS: CMake list of relative/basename paths (same length as INPUT_FILES)
12+
13+
if(NOT DEFINED OUTPUT)
14+
message(FATAL_ERROR "GenerateEmbeddedStandaloneCPP: OUTPUT is required")
15+
endif()
16+
if(NOT DEFINED INPUT_FILES)
17+
message(FATAL_ERROR "GenerateEmbeddedStandaloneCPP: INPUT_FILES is required")
18+
endif()
19+
if(NOT DEFINED REL_PATHS)
20+
message(FATAL_ERROR "GenerateEmbeddedStandaloneCPP: REL_PATHS is required")
21+
endif()
22+
23+
list(LENGTH INPUT_FILES _n_inputs)
24+
list(LENGTH REL_PATHS _n_rels)
25+
if(NOT _n_inputs EQUAL _n_rels)
26+
message(FATAL_ERROR "GenerateEmbeddedStandaloneCPP: INPUT_FILES and REL_PATHS must have same length")
27+
endif()
28+
29+
file(WRITE "${OUTPUT}" "//===- EmbeddedStandaloneCPP.cpp ------------------------------*- C++ -*-===//\n")
30+
file(APPEND "${OUTPUT}" "//\n")
31+
file(APPEND "${OUTPUT}" "// This file is generated by compiler/cmake/GenerateEmbeddedStandaloneCPP.cmake.\n")
32+
file(APPEND "${OUTPUT}" "// Do not edit manually.\n")
33+
file(APPEND "${OUTPUT}" "//\n")
34+
file(APPEND "${OUTPUT}" "#include \"mlir-tensorrt/Conversion/HostToEmitC/EmbeddedStandaloneCPP.h\"\n")
35+
file(APPEND "${OUTPUT}" "#include \"llvm/ADT/StringMap.h\"\n")
36+
file(APPEND "${OUTPUT}" "\n")
37+
file(APPEND "${OUTPUT}" "namespace mtrt::compiler::emitc_support {\n\n")
38+
39+
set(_var_names "")
40+
math(EXPR _last "${_n_inputs} - 1")
41+
foreach(i RANGE 0 ${_last})
42+
list(GET INPUT_FILES ${i} _in)
43+
list(GET REL_PATHS ${i} _rel)
44+
45+
# Sanitize a C++ identifier for the backing char array name.
46+
set(_var "${_rel}")
47+
string(REPLACE "/" "_" _var "${_var}")
48+
string(REPLACE "." "_" _var "${_var}")
49+
string(REPLACE "-" "_" _var "${_var}")
50+
51+
file(READ "${_in}" _contents)
52+
53+
file(APPEND "${OUTPUT}" "static constexpr char k_${_var}[] = R\"__MTRT_EMBED__(\n")
54+
file(APPEND "${OUTPUT}" "${_contents}")
55+
file(APPEND "${OUTPUT}" "\n)__MTRT_EMBED__\";\n\n")
56+
57+
list(APPEND _var_names "${_var}")
58+
endforeach()
59+
60+
file(APPEND "${OUTPUT}" "static const EmbeddedTextFile kFiles[] = {\n")
61+
foreach(i RANGE 0 ${_last})
62+
list(GET REL_PATHS ${i} _rel)
63+
list(GET _var_names ${i} _var)
64+
file(APPEND "${OUTPUT}" " {\"${_rel}\", llvm::StringRef(k_${_var}, sizeof(k_${_var}) - 1)},\n")
65+
endforeach()
66+
file(APPEND "${OUTPUT}" "};\n\n")
67+
68+
file(APPEND "${OUTPUT}" "llvm::ArrayRef<EmbeddedTextFile> getEmbeddedStandaloneCPPFiles() {\n")
69+
file(APPEND "${OUTPUT}" " return llvm::ArrayRef<EmbeddedTextFile>(kFiles);\n")
70+
file(APPEND "${OUTPUT}" "}\n\n")
71+
72+
file(APPEND "${OUTPUT}" "llvm::StringRef getEmbeddedStandaloneCPPFileContents(llvm::StringRef path) {\n")
73+
file(APPEND "${OUTPUT}" " for (const auto &f : kFiles) {\n")
74+
file(APPEND "${OUTPUT}" " if (f.path == path)\n")
75+
file(APPEND "${OUTPUT}" " return f.contents;\n")
76+
file(APPEND "${OUTPUT}" " }\n")
77+
file(APPEND "${OUTPUT}" " return {};\n")
78+
file(APPEND "${OUTPUT}" "}\n\n")
79+
80+
file(APPEND "${OUTPUT}" "} // namespace mtrt::compiler::emitc_support\n")

mlir-tensorrt/compiler/include/mlir-tensorrt/Compiler/Options.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/ADT/SmallVectorExtras.h"
3939
#include "llvm/Support/CommandLine.h"
4040
#include "llvm/Support/ErrorHandling.h"
41+
#include <optional>
4142
#include <string>
4243

4344
namespace mtrt::compiler {
@@ -199,6 +200,32 @@ struct EmitCOptions : public mlir::OptionsGroup {
199200

200201
static llvm::cl::OptionCategory category;
201202

203+
Option<bool> emitSupportFiles{
204+
this->ctx, "emitc-emit-support-files", llvm::cl::init(false),
205+
llvm::cl::desc(
206+
"Emit EmitC support files (runtime sources/headers, example CMake, "
207+
"and a test driver) into the artifacts directory."),
208+
llvm::cl::cat(category)};
209+
210+
Option<bool> emitRuntimeFiles{
211+
this->ctx, "emitc-emit-runtime-files", llvm::cl::init(false),
212+
llvm::cl::desc("Emit the required subset of StandaloneCPP runtime source "
213+
"and header files needed by the generated C++ code."),
214+
llvm::cl::cat(category)};
215+
216+
Option<bool> emitCMakeFile{
217+
this->ctx, "emitc-emit-cmake-file", llvm::cl::init(false),
218+
llvm::cl::desc("Emit an example CMake file for compiling the generated "
219+
"C++ code (and emitted runtime files, if requested)."),
220+
llvm::cl::cat(category)};
221+
222+
Option<bool> emitTestDriver{
223+
this->ctx, "emitc-emit-test-driver", llvm::cl::init(false),
224+
llvm::cl::desc("Emit a C++ test driver source file for building an "
225+
"executable that includes and runs the generated C++ "
226+
"code."),
227+
llvm::cl::cat(category)};
228+
202229
Option<bool> wrapModuleInEmitCClass{
203230
this->ctx, "emitc-wrap-in-class", llvm::cl::init(false),
204231
llvm::cl::desc("Wrap the module in an EmitC class"),
@@ -446,6 +473,22 @@ class MainOptions : public mlir::CLOptionScope,
446473
// Options common to all tasks
447474
//===----------------------------------------------------------------------===//
448475

476+
/// Output directory or file name for the translated host-target output.
477+
///
478+
/// This option is only registered in the global CLI scope. It is
479+
/// intentionally not registered for locally-scoped option parsing (e.g.
480+
/// `MainOptions::fromString`) to avoid duplicate option registrations and to
481+
/// keep local parsing focused on pipeline behavior rather than tool output.
482+
std::optional<Option<std::string>> outputPath;
483+
484+
/// Returns the output path value. If the option is not registered (local
485+
/// scope), returns ".".
486+
llvm::StringRef getOutputPath() const {
487+
if (outputPath)
488+
return outputPath->getValue();
489+
return ".";
490+
}
491+
449492
Option<HostTarget> hostTarget{
450493
*this,
451494
"host-target",
@@ -599,6 +642,13 @@ class MainOptions : public mlir::CLOptionScope,
599642
MainOptions(mlir::CLOptionScope::GlobalScope, ExtensionList extensions)
600643
: mlir::CLOptionScope(GlobalScope{}), extensions(std::move(extensions)),
601644
groups(mlir::make_options_group_tuple<SubGroups>(*this)) {
645+
// Register tool output path only in the global scope.
646+
if (this->isGlobalScope()) {
647+
outputPath.emplace(
648+
*this, "o", llvm::cl::desc("<output directory or file name>"),
649+
llvm::cl::init("."), llvm::cl::value_desc("directory or file"),
650+
llvm::cl::cat(category));
651+
}
602652
this->extensions.loadExtensions(*this);
603653
}
604654

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===- EmbeddedStandaloneCPP.h ----------------------------------*- C++ -*-===//
2+
//
3+
// SPDX-FileCopyrightText: Copyright 2026 NVIDIA CORPORATION & AFFILIATES.
4+
// All rights reserved.
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// Provides access to StandaloneCPP runtime source/header contents embedded in
10+
/// the compiler binary at build time.
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TENSORRT_CONVERSION_HOSTTOEMITC_EMBEDDEDSTANDALONECPP_H
14+
#define MLIR_TENSORRT_CONVERSION_HOSTTOEMITC_EMBEDDEDSTANDALONECPP_H
15+
16+
#include "llvm/ADT/ArrayRef.h"
17+
#include "llvm/ADT/StringRef.h"
18+
19+
namespace mtrt::compiler::emitc_support {
20+
21+
struct EmbeddedTextFile {
22+
llvm::StringRef path;
23+
llvm::StringRef contents;
24+
};
25+
26+
/// Returns the list of embedded StandaloneCPP runtime files.
27+
llvm::ArrayRef<EmbeddedTextFile> getEmbeddedStandaloneCPPFiles();
28+
29+
/// Returns the embedded contents for the given path, or an empty StringRef if
30+
/// not found.
31+
llvm::StringRef getEmbeddedStandaloneCPPFileContents(llvm::StringRef path);
32+
33+
} // namespace mtrt::compiler::emitc_support
34+
35+
#endif // MLIR_TENSORRT_CONVERSION_HOSTTOEMITC_EMBEDDEDSTANDALONECPP_H

mlir-tensorrt/compiler/include/mlir-tensorrt/Conversion/HostToEmitC/HostToEmitC.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,18 @@ class OpPassManager;
3131
}
3232

3333
namespace mtrt::compiler {
34+
struct EmitCOptions;
35+
36+
/// Populate the pass manager with the EmitC lowering pipeline.
37+
///
38+
/// The pipeline is parameterized by the EmitC options, and additionally takes
39+
/// the tool output path and entrypoint to enable optional emission of C++
40+
/// support files (StandaloneCPP runtime sources/headers, example CMake, and/or
41+
/// a test driver) as artifacts.
3442
void applyEmitCLoweringPipeline(mlir::OpPassManager &pm,
35-
bool wrapModuleInEmitCClass);
43+
const EmitCOptions &opts,
44+
llvm::StringRef outputPath,
45+
llvm::StringRef entrypoint);
3646
} // namespace mtrt::compiler
3747

3848
#endif // MLIR_TENSORRT_CONVERSION_HOSTTOEMITC_HOSTTOEMITC

mlir-tensorrt/compiler/include/mlir-tensorrt/Conversion/Passes.td

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,41 @@ def WrapModuleInEmitCClassPass : Pass<"wrap-module-in-emitc-class", "::mlir::Mod
132132
];
133133
}
134134

135+
def EmitCppSupportFilesPass : Pass<"emit-cpp-support-files", "::mlir::ModuleOp"> {
136+
let summary = "Emit EmitC C++ support files as executor.file_artifact ops";
137+
let description = [{
138+
This pass emits optional support files required to compile and test the C++
139+
produced by the EmitC host target. The files are emitted as
140+
`executor.file_artifact` operations, to be materialized by the
141+
`executor-serialize-artifacts` pass into the artifacts directory.
142+
143+
The emitted files can include:
144+
- StandaloneCPP runtime source and header files (subset based on runtime usage)
145+
- An example CMake file
146+
- A simple test driver
147+
}];
148+
149+
let dependentDialects = [
150+
"::mlir::emitc::EmitCDialect",
151+
"::mlir::executor::ExecutorDialect"
152+
];
153+
154+
let options = [
155+
Option<"emitRuntimeFiles", "emit-runtime-files", "bool", "false",
156+
"Emit the required subset of StandaloneCPP runtime source/header files">,
157+
Option<"emitCMakeFile", "emit-cmake-file", "bool", "false",
158+
"Emit an example CMakeLists.txt file">,
159+
Option<"emitTestDriver", "emit-test-driver", "bool", "false",
160+
"Emit a simple test driver source file">,
161+
Option<"outputFile", "output-file", "std::string", "\"\"",
162+
"The path to the generated EmitC translation output file (for driver/CMake generation)">,
163+
Option<"entrypoint", "entrypoint", "std::string", "\"main\"",
164+
"Entrypoint function name (used by the generated test driver, if present)">,
165+
Option<"supportSubdir", "support-subdir", "std::string", "\"emitc_support\"",
166+
"Subdirectory under the artifacts directory to place emitted support files">,
167+
];
168+
}
169+
135170
//===----------------------------------------------------------------------===//
136171
// TensorRTEmitCBuilderPass
137172
//===----------------------------------------------------------------------===//

mlir-tensorrt/compiler/lib/Compiler/Pipeline.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,9 @@ static void addLLVMOrEmitCTail(OpPassManager &pm, const MainOptions &options,
345345
}
346346

347347
if (hostTarget == HostTarget::EmitC) {
348-
mtrt::compiler::applyEmitCLoweringPipeline(
349-
pm, options.get<EmitCOptions>().wrapModuleInEmitCClass);
348+
mtrt::compiler::applyEmitCLoweringPipeline(pm, options.get<EmitCOptions>(),
349+
options.getOutputPath(),
350+
options.entrypoint);
350351
}
351352

352353
pm.addPass(mlir::executor::createExecutorSerializeArtifactsPass(

0 commit comments

Comments
 (0)