已合并
[AscendNPU-IR] feat: Support standalone build #21
[AscendNPU-IR] feat: Support standalone build #21
已合并
wcleungaj创建于 2025年10月10日
共 20 个文件变更+141-26
@@ -30,6 +30,10 @@ else()
30 set(BISHENGIR_ENABLE_TORCH_CONVERSIONS 0)30 set(BISHENGIR_ENABLE_TORCH_CONVERSIONS 0)
31endif()31endif()
32 32 
chenxiangting
chenxiangtingchenxiangting2025年10月14日

在这里定义

option(BISHENGIR_BUILD_STANDALONE_IR_ONLY
  "Experimental feature. If set, only build IR definitions." OFF)
likedislike
33+# The BISHENGIR_BUILD_STANDALONE_IR_ONLY option is used to only build IR definitions.
34+option(BISHENGIR_BUILD_STANDALONE_IR_ONLY
35+ "Experimental feature. If set, only build IR definitions." OFF)
36+ 
33# Only enable execution engine if the native target is available.37# Only enable execution engine if the native target is available.
34if (${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)38if (${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
35 set(MLIR_ENABLE_EXECUTION_ENGINE 1)39 set(MLIR_ENABLE_EXECUTION_ENGINE 1)
@@ -144,8 +148,29 @@ add_llvm_install_targets(install-bishengir-publish-products
144 COMPONENT bishengir-publish-products)148 COMPONENT bishengir-publish-products)
145 149 
146add_subdirectory(bishengir/tools/bishengir-hfusion-ods-gen)150add_subdirectory(bishengir/tools/bishengir-hfusion-ods-gen)
147-add_subdirectory(bishengir/include/bishengir)151+if (BISHENGIR_BUILD_STANDALONE_IR_ONLY)
148-add_subdirectory(bishengir/lib)152+ add_subdirectory(bishengir/include/bishengir/Interfaces)
153+ set(STANDALONE_IR_BUILD_DIALECT
154+ Annotation
155+ HFusion
156+ HIVM
157+ MemRef
158+ MemRefExt
159+ Symbol
160+ Tensor
161+ )
162+ foreach(d IN LISTS STANDALONE_IR_BUILD_DIALECT)
163+ add_subdirectory(bishengir/include/bishengir/Dialect/${d})
164+ add_subdirectory(bishengir/lib/Dialect/${d}/IR)
165+ endforeach()
166+ # TODO: Math and Utils should be added in STANDALONE_IR_BUILD_DIALECT
167+ add_subdirectory(bishengir/include/bishengir/Dialect/MathExt)
168+ add_subdirectory(bishengir/lib/Dialect/Math)
169+ add_subdirectory(bishengir/lib/Dialect/Utils)
170+else()
171+ add_subdirectory(bishengir/include/bishengir)
172+ add_subdirectory(bishengir/lib)
173+endif()
149add_subdirectory(bishengir/lib/CAPI)174add_subdirectory(bishengir/lib/CAPI)
150 175 
151if(MLIR_INCLUDE_TESTS)176if(MLIR_INCLUDE_TESTS)
@@ -163,7 +188,9 @@ if(MLIR_ENABLE_BINDINGS_PYTHON)
163 add_subdirectory(python)188 add_subdirectory(python)
164endif()189endif()
165 190 
166-add_subdirectory(bishengir/tools)191+if(NOT BISHENGIR_BUILD_STANDALONE_IR_ONLY)
192+ add_subdirectory(bishengir/tools)
193+endif()
167 194 
168if(NOT LLVM_INSTALL_TOOLCHAIN_ONLY)195if(NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
169 install(196 install(
@@ -34,4 +34,7 @@
34/* If set, enable BiShengIR CPU Runner. */34/* If set, enable BiShengIR CPU Runner. */
35#cmakedefine01 MLIR_ENABLE_EXECUTION_ENGINE35#cmakedefine01 MLIR_ENABLE_EXECUTION_ENGINE
36 36 
37+/* If set, only build IR definitions. */
38+#cmakedefine01 BISHENGIR_BUILD_STANDALONE_IR_ONLY
39+ 
37#endif40#endif
@@ -1,3 +1,8 @@
1+set(tblgen_feat_list)
2+if(BISHENGIR_BUILD_STANDALONE_IR_ONLY)
3+ list(APPEND tblgen_feat_list -DBISHENGIR_BUILD_STANDALONE_IR_ONLY)
4+endif()
5+ 
1# Declare a function to generate ODS with bishengir-hfusion-ods-yaml-gen6# Declare a function to generate ODS with bishengir-hfusion-ods-yaml-gen
2function(add_hfusion_ods_yaml_gen yaml_ast_file output_file)7function(add_hfusion_ods_yaml_gen yaml_ast_file output_file)
3 set(YAML_AST_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/${yaml_ast_file})8 set(YAML_AST_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/${yaml_ast_file})
@@ -42,7 +47,16 @@ add_custom_target(HFusionOdsGen
42)47)
43add_dependencies(mlir-headers HFusionOdsGen)48add_dependencies(mlir-headers HFusionOdsGen)
44 49 
45-add_bishengir_dialect(HFusionOps hfusion)50+# TODO: Better to do it with the add_bishengir_dialect()
51+set(LLVM_TARGET_DEFINITIONS HFusionOps.td)
52+mlir_tablegen(HFusionOps.h.inc -gen-op-decls)
53+mlir_tablegen(HFusionOps.cpp.inc -gen-op-defs)
54+mlir_tablegen(HFusionOpsTypes.h.inc -gen-typedef-decls -typedefs-dialect=hfusion)
55+mlir_tablegen(HFusionOpsTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=hfusion)
56+mlir_tablegen(HFusionOpsDialect.h.inc -gen-dialect-decls -dialect=hfusion ${tblgen_feat_list})
57+mlir_tablegen(HFusionOpsDialect.cpp.inc -gen-dialect-defs -dialect=hfusion ${tblgen_feat_list})
58+add_public_tablegen_target(BiShengIRHFusionOpsIncGen)
59+add_dependencies(mlir-headers BiShengIRHFusionOpsIncGen)
46 60 
47add_bishengir_doc(HFusionDoc HFusionDialect Dialects/ -gen-dialect-doc -dialect=hfusion)61add_bishengir_doc(HFusionDoc HFusionDialect Dialects/ -gen-dialect-doc -dialect=hfusion)
48add_dependencies(HFusionDialectDocGen HFusionOdsGen)62add_dependencies(HFusionDialectDocGen HFusionOdsGen)
@@ -34,7 +34,9 @@ def HFusion_Dialect : Dialect {
34 }];34 }];
35 let cppNamespace = "::mlir::hfusion";35 let cppNamespace = "::mlir::hfusion";
36 let dependentDialects = [36 let dependentDialects = [
37+#ifndef BISHENGIR_BUILD_STANDALONE_IR_ONLY
37 "hacc::HACCDialect",38 "hacc::HACCDialect",
39+#endif
38 "linalg::LinalgDialect",40 "linalg::LinalgDialect",
39 "mathExt::MathExtDialect",41 "mathExt::MathExtDialect",
40 "mesh::MeshDialect",42 "mesh::MeshDialect",
@@ -1,6 +1,11 @@
1+set(tblgen_feat_list)
2+if(BISHENGIR_BUILD_STANDALONE_IR_ONLY)
3+ list(APPEND tblgen_feat_list -DBISHENGIR_BUILD_STANDALONE_IR_ONLY)
4+endif()
5+ 
1set(LLVM_TARGET_DEFINITIONS HIVMOps.td)6set(LLVM_TARGET_DEFINITIONS HIVMOps.td)
2-mlir_tablegen(HIVMDialect.h.inc -gen-dialect-decls -dialect=hivm)7+mlir_tablegen(HIVMDialect.h.inc -gen-dialect-decls -dialect=hivm ${tblgen_feat_list})
chenxiangting
chenxiangtingchenxiangting2025年10月14日

hivm同理

likedislike
3-mlir_tablegen(HIVMDialect.cpp.inc -gen-dialect-defs -dialect=hivm)8+mlir_tablegen(HIVMDialect.cpp.inc -gen-dialect-defs -dialect=hivm ${tblgen_feat_list})
4mlir_tablegen(HIVMOps.h.inc -gen-op-decls)9mlir_tablegen(HIVMOps.h.inc -gen-op-decls)
5mlir_tablegen(HIVMOps.cpp.inc -gen-op-defs)10mlir_tablegen(HIVMOps.cpp.inc -gen-op-defs)
6add_public_tablegen_target(BiShengIRHIVMIncGen)11add_public_tablegen_target(BiShengIRHIVMIncGen)
@@ -41,7 +41,10 @@ def HIVM_Dialect : Dialect {
41 let cppNamespace = "::mlir::hivm";41 let cppNamespace = "::mlir::hivm";
42 let dependentDialects = [42 let dependentDialects = [
43 "arith::ArithDialect", "bishengir::memref_ext::MemRefExtDialect",43 "arith::ArithDialect", "bishengir::memref_ext::MemRefExtDialect",
44- "math::MathDialect", "memref::MemRefDialect", "hacc::HACCDialect",44+ "math::MathDialect", "memref::MemRefDialect",
45+#ifndef BISHENGIR_BUILD_STANDALONE_IR_ONLY
46+ "hacc::HACCDialect",
47+#endif
45 "tensor::TensorDialect"48 "tensor::TensorDialect"
46 ];49 ];
47 let hasCanonicalizer = 1;50 let hasCanonicalizer = 1;
@@ -27,7 +27,10 @@
27#include "bishengir/Dialect/Annotation/IR/Annotation.h"27#include "bishengir/Dialect/Annotation/IR/Annotation.h"
28#include "bishengir/Dialect/Annotation/Transforms/BufferizableOpInterfaceImpl.h"28#include "bishengir/Dialect/Annotation/Transforms/BufferizableOpInterfaceImpl.h"
29#include "bishengir/Dialect/Bufferization/Transforms/TilingInterfaceImpl.h"29#include "bishengir/Dialect/Bufferization/Transforms/TilingInterfaceImpl.h"
30+#include "bishengir/Config/bishengir-config.h"
31+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
30#include "bishengir/Dialect/HACC/IR/HACC.h"32#include "bishengir/Dialect/HACC/IR/HACC.h"
33+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
31#include "bishengir/Dialect/HFusion/IR/HFusion.h"34#include "bishengir/Dialect/HFusion/IR/HFusion.h"
32#include "bishengir/Dialect/HFusion/Transforms/BufferizableOpInterfaceImpl.h"35#include "bishengir/Dialect/HFusion/Transforms/BufferizableOpInterfaceImpl.h"
33#include "bishengir/Dialect/HFusion/Transforms/DecomposeOpInterfaceImpl.h"36#include "bishengir/Dialect/HFusion/Transforms/DecomposeOpInterfaceImpl.h"
@@ -52,7 +55,9 @@ namespace bishengir {
52inline void registerAllDialects(mlir::DialectRegistry &registry) {55inline void registerAllDialects(mlir::DialectRegistry &registry) {
53 // clang-format off56 // clang-format off
54 registry.insert<mlir::annotation::AnnotationDialect,57 registry.insert<mlir::annotation::AnnotationDialect,
58+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
55 mlir::hacc::HACCDialect,59 mlir::hacc::HACCDialect,
60+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
56 mlir::hfusion::HFusionDialect,61 mlir::hfusion::HFusionDialect,
57 mlir::hivm::HIVMDialect,62 mlir::hivm::HIVMDialect,
58 mlir::mathExt::MathExtDialect,63 mlir::mathExt::MathExtDialect,
@@ -68,13 +73,16 @@ inline void registerAllDialects(mlir::DialectRegistry &registry) {
68 // clang-format on73 // clang-format on
69#endif74#endif
70 75 
76+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
chenxiangting
chenxiangtingchenxiangting2025年10月14日
// TODO: separate to bishengir::registerAllDialectExternalModels for better modularity
likedislike
71 // Register all external models.77 // Register all external models.
78+ // TODO: separate to bishengir::registerAllDialectExternalModels for better modularity
72 mlir::annotation::registerBufferizableOpInterfaceExternalModels(registry);79 mlir::annotation::registerBufferizableOpInterfaceExternalModels(registry);
73 mlir::hfusion::registerTilingInterfaceExternalModels(registry);80 mlir::hfusion::registerTilingInterfaceExternalModels(registry);
74 mlir::hfusion::registerBufferizableOpInterfaceExternalModels(registry);81 mlir::hfusion::registerBufferizableOpInterfaceExternalModels(registry);
75 mlir::hfusion::registerDecomposeInterfaceExternalModels(registry);82 mlir::hfusion::registerDecomposeInterfaceExternalModels(registry);
76 bishengir::tensor::registerTilingInterfaceExternalModels(registry);83 bishengir::tensor::registerTilingInterfaceExternalModels(registry);
77 bishengir::bufferization::registerTilingInterfaceExternalModels(registry);84 bishengir::bufferization::registerTilingInterfaceExternalModels(registry);
85+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
78}86}
79 87 
80/// Append all the bishengir-specific dialects to the registry contained in the88/// Append all the bishengir-specific dialects to the registry contained in the
@@ -1,2 +1,5 @@
1+if(BISHENGIR_BUILD_STANDALONE_IR_ONLY)
2+ return()
3+endif()
1add_subdirectory(Dialect)4add_subdirectory(Dialect)
2add_subdirectory(RegisterEverything)5add_subdirectory(RegisterEverything)
@@ -1,3 +1,8 @@
1+if(NOT BISHENGIR_BUILD_STANDALONE_IR_ONLY)
2+ list(APPEND BS_FEAT_LINK_LIBS BiShengIRLinalgDialectExt)
3+ list(APPEND BS_FEAT_LINK_LIBS BiShengIRHACCDialect)
4+endif()
5+ 
1add_bishengir_dialect_library(BiShengIRHFusionDialect6add_bishengir_dialect_library(BiShengIRHFusionDialect
2 HFusionDialect.cpp7 HFusionDialect.cpp
3 HFusionImpl.cpp8 HFusionImpl.cpp
@@ -14,9 +19,8 @@ add_bishengir_dialect_library(BiShengIRHFusionDialect
14 BiShengIRHFusionStructuredOpsIncGen19 BiShengIRHFusionStructuredOpsIncGen
15 20 
16 LINK_LIBS PUBLIC21 LINK_LIBS PUBLIC
17- BiShengIRLinalgDialectExt22+ ${BS_FEAT_LINK_LIBS}
18 BiShengIRSymbolDialect23 BiShengIRSymbolDialect
19- BiShengIRHACCDialect
20 BiShengIRMathExtDialect24 BiShengIRMathExtDialect
21 MLIRBufferizationDialect25 MLIRBufferizationDialect
22 MLIRDestinationStyleOpInterface26 MLIRDestinationStyleOpInterface
@@ -14,8 +14,11 @@
14// limitations under the License.14// limitations under the License.
15//15//
16//===----------------------------------------------------------------------===//16//===----------------------------------------------------------------------===//
17+#include "bishengir/Config/bishengir-config.h"
17 18 
19+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
18#include "bishengir/Dialect/HACC/IR/HACC.h"20#include "bishengir/Dialect/HACC/IR/HACC.h"
21+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
19#include "bishengir/Dialect/HFusion/IR/HFusion.h"22#include "bishengir/Dialect/HFusion/IR/HFusion.h"
20#include "bishengir/Dialect/MathExt/IR/MathExt.h"23#include "bishengir/Dialect/MathExt/IR/MathExt.h"
21 24 
@@ -15,6 +15,7 @@
15//15//
16//===----------------------------------------------------------------------===//16//===----------------------------------------------------------------------===//
17 17 
18+#include "bishengir/Config/bishengir-config.h"
18#include "bishengir/Dialect/HFusion/IR/HFusion.h"19#include "bishengir/Dialect/HFusion/IR/HFusion.h"
19#include "bishengir/Dialect/HFusion/IR/HFusionImpl.h"20#include "bishengir/Dialect/HFusion/IR/HFusionImpl.h"
20#include "bishengir/Dialect/MathExt/IR/MathExt.h"21#include "bishengir/Dialect/MathExt/IR/MathExt.h"
@@ -1969,12 +1970,16 @@ FailureOr<SmallVector<Value>> IsFiniteOp::decomposeOperation(OpBuilder &b) {
1969MutableOperandRange GatherOp::getDpsInitsMutable() { return getInitMutable(); }1970MutableOperandRange GatherOp::getDpsInitsMutable() { return getInitMutable(); }
1970 1971 
1971SmallVector<utils::IteratorType> GatherOp::getIteratorTypesArray() {1972SmallVector<utils::IteratorType> GatherOp::getIteratorTypesArray() {
1973+#if BISHENGIR_BUILD_STANDALONE_IR_ONLY
1974+ llvm_unreachable("Not implemented");
1975+#else
1972 SmallVector<utils::IteratorType> result(getInit().getType().getRank() + 1,1976 SmallVector<utils::IteratorType> result(getInit().getType().getRank() + 1,
1973 utils::IteratorType::parallel);1977 utils::IteratorType::parallel);
1974 // The gather dim for indicies and src each take a loop, since we want the src1978 // The gather dim for indicies and src each take a loop, since we want the src
1975 // loop (reduction dim) to be on the inside, we set the gatherDim+1 to reduce1979 // loop (reduction dim) to be on the inside, we set the gatherDim+1 to reduce
1976 result[getAxis() + 1] = utils::IteratorType::gather;1980 result[getAxis() + 1] = utils::IteratorType::gather;
1977 return result;1981 return result;
1982+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
1978}1983}
1979 1984 
1980/// The source gather axis will be inside the index gather axis. For src1985/// The source gather axis will be inside the index gather axis. For src
@@ -2299,6 +2304,7 @@ void AtomicXchgOp::getEffects(
2299//===----------------------------------------------------------------------===//2304//===----------------------------------------------------------------------===//
2300void HFusionDialect::getCanonicalizationPatterns(2305void HFusionDialect::getCanonicalizationPatterns(
2301 RewritePatternSet &results) const {2306 RewritePatternSet &results) const {
2307+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
2302 results.add<2308 results.add<
2303 mlir::linalg::InlineDenseSplatToGenericRegion<hfusion::ElemwiseBinaryOp>,2309 mlir::linalg::InlineDenseSplatToGenericRegion<hfusion::ElemwiseBinaryOp>,
2304 mlir::linalg::InlineDenseSplatToGenericRegion<hfusion::ElemwiseUnaryOp>,2310 mlir::linalg::InlineDenseSplatToGenericRegion<hfusion::ElemwiseUnaryOp>,
@@ -2307,6 +2313,7 @@ void HFusionDialect::getCanonicalizationPatterns(
2307 mlir::linalg::SimplifySplatDenseForBinary<hfusion::ElemwiseBinaryOp>,2313 mlir::linalg::SimplifySplatDenseForBinary<hfusion::ElemwiseBinaryOp>,
2308 mlir::linalg::SimplifySplatDenseForBinary<hfusion::CompareOp>>(2314 mlir::linalg::SimplifySplatDenseForBinary<hfusion::CompareOp>>(
2309 getContext());2315 getContext());
2316+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
2310}2317}
2311 2318 
2312//===----------------------------------------------------------------------===//2319//===----------------------------------------------------------------------===//
@@ -2330,8 +2337,8 @@ LogicalResult SortOp::verify() {
2330// HistogramOp2337// HistogramOp
2331//===----------------------------------------------------------------------===//2338//===----------------------------------------------------------------------===//
2332LogicalResult HistogramOp::verify() {2339LogicalResult HistogramOp::verify() {
2333- auto inTy = getInput().getType().dyn_cast<RankedTensorType>();2340+ auto inTy = dyn_cast<RankedTensorType>(getInput().getType());
2334- auto outTy = getOutput().getType().dyn_cast<RankedTensorType>();2341+ auto outTy = dyn_cast<RankedTensorType>(getOutput().getType());
2335 Value mask = getMask();2342 Value mask = getMask();
2336 2343 
2337 // Input/output must be ranked tensors2344 // Input/output must be ranked tensors
@@ -2340,7 +2347,7 @@ LogicalResult HistogramOp::verify() {
2340 2347 
2341 // Input element type must be i32 or i642348 // Input element type must be i32 or i64
2342 Type inEltTy = inTy.getElementType();2349 Type inEltTy = inTy.getElementType();
2343- if (!inEltTy.isa<IntegerType>() ||2350+ if (!isa<IntegerType>(inEltTy) ||
2344 (inEltTy.getIntOrFloatBitWidth() != 32 &&2351 (inEltTy.getIntOrFloatBitWidth() != 32 &&
2345 inEltTy.getIntOrFloatBitWidth() != 64))2352 inEltTy.getIntOrFloatBitWidth() != 64))
2346 return emitOpError() << "input element type must be i32 or i64";2353 return emitOpError() << "input element type must be i32 or i64";
@@ -2353,7 +2360,7 @@ LogicalResult HistogramOp::verify() {
2353 2360 
2354 // Output element type must be i32 or i642361 // Output element type must be i32 or i64
2355 Type outEltTy = outTy.getElementType();2362 Type outEltTy = outTy.getElementType();
2356- if (!outEltTy.isa<IntegerType>() ||2363+ if (!isa<IntegerType>(outEltTy) ||
2357 (outEltTy.getIntOrFloatBitWidth() != 32 &&2364 (outEltTy.getIntOrFloatBitWidth() != 32 &&
2358 outEltTy.getIntOrFloatBitWidth() != 64))2365 outEltTy.getIntOrFloatBitWidth() != 64))
2359 return emitOpError() << "output element type must be i32 or i64";2366 return emitOpError() << "output element type must be i32 or i64";
@@ -2366,7 +2373,7 @@ LogicalResult HistogramOp::verify() {
2366 2373 
2367 // If mask is provided, it must match input shape2374 // If mask is provided, it must match input shape
2368 if (mask) {2375 if (mask) {
2369- auto maskTy = mask.getType().dyn_cast<RankedTensorType>();2376+ auto maskTy = dyn_cast<RankedTensorType>(mask.getType());
2370 if (!maskTy)2377 if (!maskTy)
2371 return emitOpError() << "mask must be a ranked tensor";2378 return emitOpError() << "mask must be a ranked tensor";
2372 if (maskTy.getElementType() != IntegerType::get(getContext(), 1))2379 if (maskTy.getElementType() != IntegerType::get(getContext(), 1))
@@ -2387,8 +2394,8 @@ FailureOr<SmallVector<Value>> HistogramOp::decomposeOperation(OpBuilder &b) {
2387 Value input = getInput();2394 Value input = getInput();
2388 int64_t bins = getNumBins();2395 int64_t bins = getNumBins();
2389 Value mask = getMask();2396 Value mask = getMask();
2390- auto inTy = input.getType().cast<RankedTensorType>();2397+ auto inTy = cast<RankedTensorType>(input.getType());
2391- auto outTy = getOutput().getType().cast<RankedTensorType>();2398+ auto outTy = cast<RankedTensorType>(getOutput().getType());
2392 2399 
2393 Type outEltTy = outTy.getElementType();2400 Type outEltTy = outTy.getElementType();
2394 Type idxTy = b.getIndexType();2401 Type idxTy = b.getIndexType();
@@ -15,6 +15,7 @@
15//15//
16//===----------------------------------------------------------------------===//16//===----------------------------------------------------------------------===//
17 17 
18+#include "bishengir/Config/bishengir-config.h"
18#include "bishengir/Dialect/HIVM/IR/HIVM.h"19#include "bishengir/Dialect/HIVM/IR/HIVM.h"
19#include "bishengir/Dialect/Tensor/IR/TensorImpl.h"20#include "bishengir/Dialect/Tensor/IR/TensorImpl.h"
20#include "bishengir/Dialect/Utils/Util.h"21#include "bishengir/Dialect/Utils/Util.h"
@@ -505,7 +506,11 @@ void VBrcOp::getCanonicalizationPatterns(::mlir::RewritePatternSet &results,
505 506 
506void VReduceOp::getCanonicalizationPatterns(::mlir::RewritePatternSet &results,507void VReduceOp::getCanonicalizationPatterns(::mlir::RewritePatternSet &results,
507 ::mlir::MLIRContext *context) {508 ::mlir::MLIRContext *context) {
508- results.add<RedudantVReduceOp, RedudantVReduceInitOp>(context);509+ results.add<RedudantVReduceOp
510+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
511+ , RedudantVReduceInitOp
512+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
513+ >(context);
509}514}
510 515 
511void VCumsumOp::getCanonicalizationPatterns(::mlir::RewritePatternSet &results,516void VCumsumOp::getCanonicalizationPatterns(::mlir::RewritePatternSet &results,
@@ -15,8 +15,11 @@
15//15//
16//===----------------------------------------------------------------------===//16//===----------------------------------------------------------------------===//
17 17 
18+#include "bishengir/Config/bishengir-config.h"
18#include "bishengir/Dialect/HIVM/IR/HIVM.h"19#include "bishengir/Dialect/HIVM/IR/HIVM.h"
20+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
19#include "bishengir/Dialect/HACC/IR/HACC.h"21#include "bishengir/Dialect/HACC/IR/HACC.h"
22+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
20#include "bishengir/Dialect/HIVM/IR/HIVMImpl.h"23#include "bishengir/Dialect/HIVM/IR/HIVMImpl.h"
21#include "bishengir/Dialect/HIVM/IR/HIVMInterfaces.h"24#include "bishengir/Dialect/HIVM/IR/HIVMInterfaces.h"
22#include "bishengir/Dialect/HIVM/Utils/Utils.h"25#include "bishengir/Dialect/HIVM/Utils/Utils.h"
@@ -15,8 +15,11 @@
15//15//
16//===----------------------------------------------------------------------===//16//===----------------------------------------------------------------------===//
17 17 
18+#include "bishengir/Config/bishengir-config.h"
18#include "bishengir/Dialect/HIVM/IR/HIVM.h"19#include "bishengir/Dialect/HIVM/IR/HIVM.h"
20+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
19#include "bishengir/Dialect/HACC/IR/HACC.h"21#include "bishengir/Dialect/HACC/IR/HACC.h"
22+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
20#include "bishengir/Dialect/HIVM/IR/HIVMImpl.h"23#include "bishengir/Dialect/HIVM/IR/HIVMImpl.h"
21#include "bishengir/Dialect/HIVM/IR/HIVMInterfaces.h"24#include "bishengir/Dialect/HIVM/IR/HIVMInterfaces.h"
22#include "bishengir/Dialect/HIVM/Utils/Utils.h"25#include "bishengir/Dialect/HIVM/Utils/Utils.h"
@@ -18,7 +18,6 @@
18#include "bishengir/Dialect/MathExt/IR/MathExt.h"18#include "bishengir/Dialect/MathExt/IR/MathExt.h"
19#include "mlir/Dialect/Arith/IR/Arith.h"19#include "mlir/Dialect/Arith/IR/Arith.h"
20#include "mlir/Dialect/CommonFolders.h"20#include "mlir/Dialect/CommonFolders.h"
21-#include "mlir/Dialect/Quant/QuantOps.h"
22#include "mlir/Dialect/Tensor/IR/Tensor.h"21#include "mlir/Dialect/Tensor/IR/Tensor.h"
23#include "mlir/Dialect/UB/IR/UBOps.h"22#include "mlir/Dialect/UB/IR/UBOps.h"
24#include "mlir/IR/BuiltinTypes.h"23#include "mlir/IR/BuiltinTypes.h"
@@ -15,7 +15,10 @@
15//15//
16//===----------------------------------------------------------------------===//16//===----------------------------------------------------------------------===//
17 17 
18+#include "bishengir/Config/bishengir-config.h"
19+ 
18#include "bishengir/Dialect/Tensor/IR/TensorImpl.h"20#include "bishengir/Dialect/Tensor/IR/TensorImpl.h"
21+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
19#include "mlir/Dialect/Linalg/IR/LinalgExtensions.h"22#include "mlir/Dialect/Linalg/IR/LinalgExtensions.h"
20#include "llvm/ADT/SmallVectorExtras.h"23#include "llvm/ADT/SmallVectorExtras.h"
21 24 
@@ -46,3 +49,5 @@ Value createTensorEmptyOp(OpBuilder &builder, Location loc, Value source) {
46 49 
47} // namespace tensor50} // namespace tensor
48} // namespace mlir51} // namespace mlir
52+ 
53+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
@@ -1,3 +1,7 @@
1+if(NOT BISHENGIR_BUILD_STANDALONE_IR_ONLY)
2+ list(APPEND BS_FEAT_LINK_LIBS BiShengIRLinalgDialectExt)
3+endif()
4+ 
1add_bishengir_library(BiShengIRDialectUtils5add_bishengir_library(BiShengIRDialectUtils
2 OpInterfaceUtils.cpp6 OpInterfaceUtils.cpp
3 ReachabilityAnalyzer.cpp7 ReachabilityAnalyzer.cpp
@@ -6,10 +10,7 @@ add_bishengir_library(BiShengIRDialectUtils
6 ValueDependencyAnalyzer.cpp10 ValueDependencyAnalyzer.cpp
7 11 
8 DEPENDS12 DEPENDS
9- MLIRLinalgOpsIncGen
10- MLIRLinalgStructuredOpsIncGen
11 BiShengIRHIVMEnumsIncGen13 BiShengIRHIVMEnumsIncGen
12- BiShengIRHIVMAttributesIncGen
13 14 
14 LINK_LIBS PUBLIC15 LINK_LIBS PUBLIC
15 BiShengIRAnnotationDialect16 BiShengIRAnnotationDialect
@@ -24,5 +25,5 @@ add_bishengir_library(BiShengIRDialectUtils
24 MLIRTensorDialect25 MLIRTensorDialect
25 BiShengIRMemRefDialect26 BiShengIRMemRefDialect
26 BiShengIRTensorDialect27 BiShengIRTensorDialect
27- BiShengIRLinalgDialectExt28+ ${BS_FEAT_LINK_LIBS}
28)29)
@@ -25,6 +25,8 @@
25// https://github.com/bytedance/byteir/blob/main/compiler/lib/Utils/OpInterfaceUtils.cpp25// https://github.com/bytedance/byteir/blob/main/compiler/lib/Utils/OpInterfaceUtils.cpp
26//===----------------------------------------------------------------------===//26//===----------------------------------------------------------------------===//
27 27 
28+#include "bishengir/Config/bishengir-config.h"
29+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
28#include "bishengir/Dialect/Utils/OpInterfaceUtils.h"30#include "bishengir/Dialect/Utils/OpInterfaceUtils.h"
29 31 
30#include "mlir/IR/BuiltinTypes.h"32#include "mlir/IR/BuiltinTypes.h"
@@ -97,4 +99,6 @@ void mlir::detail::addOpInterfaceExtension(
97 99 
98void mlir::registerOpInterfaceExtensions(DialectRegistry &registry) {100void mlir::registerOpInterfaceExtensions(DialectRegistry &registry) {
99 ExtensionRegistry::inst().apply(registry);101 ExtensionRegistry::inst().apply(registry);
100-}102+}
103+ 
104+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
@@ -15,13 +15,16 @@
15//15//
16//===----------------------------------------------------------------------===//16//===----------------------------------------------------------------------===//
17 17 
18-#include "bishengir/Dialect/Utils/Util.h"18+#include "bishengir/Config/bishengir-config.h"
19#include "bishengir/Dialect/Annotation/IR/Annotation.h"19#include "bishengir/Dialect/Annotation/IR/Annotation.h"
20#include "bishengir/Dialect/HIVM/Utils/Utils.h"20#include "bishengir/Dialect/HIVM/Utils/Utils.h"
21#include "bishengir/Dialect/MemRef/IR/MemRefImpl.h"21#include "bishengir/Dialect/MemRef/IR/MemRefImpl.h"
22#include "bishengir/Dialect/Tensor/IR/TensorImpl.h"22#include "bishengir/Dialect/Tensor/IR/TensorImpl.h"
23#include "mlir/Dialect/Bufferization/IR/Bufferization.h"23#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
24+#include "bishengir/Dialect/Utils/Util.h"
25+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
24#include "mlir/Dialect/Linalg/IR/LinalgExtensions.h"26#include "mlir/Dialect/Linalg/IR/LinalgExtensions.h"
27+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
25#include "mlir/Dialect/SCF/IR/SCF.h"28#include "mlir/Dialect/SCF/IR/SCF.h"
26#include "llvm/ADT/TypeSwitch.h"29#include "llvm/ADT/TypeSwitch.h"
27 30 
@@ -164,8 +167,13 @@ Value createEmptyOpWithTargetElemType(
164 std::optional<MemRefLayoutAttrInterface> layout) {167 std::optional<MemRefLayoutAttrInterface> layout) {
165 auto shapedType = cast<ShapedType>(source.getType());168 auto shapedType = cast<ShapedType>(source.getType());
166 if (isa<TensorType>(shapedType)) {169 if (isa<TensorType>(shapedType)) {
170+#if BISHENGIR_BUILD_STANDALONE_IR_ONLY
171+ llvm_unreachable("Not implemented");
172+#else
173+ // TODO: it should be defined in Dialect/Tensor/IR
167 return tensor::createTensorEmptyOpWithTargetElemType(builder, loc, source,174 return tensor::createTensorEmptyOpWithTargetElemType(builder, loc, source,
chenxiangting
chenxiangtingchenxiangting2025年10月14日

这里为什么用宏隔离了

likedislike
wcleungaj
wcleungaj
2025年10月14日 评论:
chenxiangting
chenxiangting
2025年10月15日 评论:
168 targetElemType);175 targetElemType);
176+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
169 }177 }
170 return memref::createMemRefAllocOpWithTargetElemType(builder, loc, source,178 return memref::createMemRefAllocOpWithTargetElemType(builder, loc, source,
171 targetElemType, std::move(layout));179 targetElemType, std::move(layout));
@@ -174,7 +182,11 @@ Value createEmptyOpWithTargetElemType(
174Value createEmptyOp(OpBuilder &builder, Location loc, Value source) {182Value createEmptyOp(OpBuilder &builder, Location loc, Value source) {
175 auto shapedType = cast<ShapedType>(source.getType());183 auto shapedType = cast<ShapedType>(source.getType());
176 if (isa<TensorType>(shapedType)) {184 if (isa<TensorType>(shapedType)) {
185+#if BISHENGIR_BUILD_STANDALONE_IR_ONLY
186+ llvm_unreachable("Not implemented");
187+#else
177 return tensor::createTensorEmptyOp(builder, loc, source);188 return tensor::createTensorEmptyOp(builder, loc, source);
189+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
178 }190 }
179 return memref::createMemRefAllocOp(builder, loc, source);191 return memref::createMemRefAllocOp(builder, loc, source);
180}192}
@@ -719,6 +731,7 @@ void utils::renumberReassociation(
719 }731 }
720}732}
721 733 
734+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
722bool utils::isScalarLike(Value value) {735bool utils::isScalarLike(Value value) {
723 Type type = value.getType();736 Type type = value.getType();
724 std::optional<size_t> rankMaybe = utils::getShapeRank(type);737 std::optional<size_t> rankMaybe = utils::getShapeRank(type);
@@ -738,6 +751,7 @@ bool utils::isScalarLike(Value value) {
738 // for one size tensor like tensor<1x1x1xf32>751 // for one size tensor like tensor<1x1x1xf32>
739 return isOneSizeShape(value);752 return isOneSizeShape(value);
740}753}
754+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
741 755 
742bool utils::isOneSizeShape(Value value) {756bool utils::isOneSizeShape(Value value) {
743 if (auto shapedType = dyn_cast<ShapedType>(value.getType())) {757 if (auto shapedType = dyn_cast<ShapedType>(value.getType())) {
@@ -747,6 +761,7 @@ bool utils::isOneSizeShape(Value value) {
747 return false;761 return false;
748}762}
749 763 
764+#if (!BISHENGIR_BUILD_STANDALONE_IR_ONLY)
750std::optional<Value> utils::extractScalarValue(PatternRewriter &rewriter,765std::optional<Value> utils::extractScalarValue(PatternRewriter &rewriter,
751 Location loc, Value src) {766 Location loc, Value src) {
752 Type type = src.getType();767 Type type = src.getType();
@@ -775,6 +790,7 @@ std::optional<Value> utils::extractScalarValue(PatternRewriter &rewriter,
775 }790 }
776 return std::nullopt;791 return std::nullopt;
777}792}
793+#endif // BISHENGIR_BUILD_STANDALONE_IR_ONLY
778 794 
779bool utils::isArithOp(Operation *op) {795bool utils::isArithOp(Operation *op) {
780 if (op == nullptr) {796 if (op == nullptr) {
@@ -677,7 +677,7 @@ ArrayAttr {0}::getIndexingMaps() {{
677 MLIRContext *context = getContext();677 MLIRContext *context = getContext();
678 auto symbolBindings = getSymbolBindings(*this);678 auto symbolBindings = getSymbolBindings(*this);
679 SmallVector<AffineMap> maps;679 SmallVector<AffineMap> maps;
680- {2}680+ {1}
681 cached = Builder(context).getAffineMapArrayAttr(maps);681 cached = Builder(context).getAffineMapArrayAttr(maps);
682 getOperation()->setAttr(memoizeAttr, cached);682 getOperation()->setAttr(memoizeAttr, cached);
683 return cached;683 return cached;
@@ -984,7 +984,7 @@ exprs.push_back(getAffineConstantExpr(cst{1}, context));
984 // TODO: This needs to be memoized and/or converted to non-parser based984 // TODO: This needs to be memoized and/or converted to non-parser based
985 // C++ codegen prior to real use.985 // C++ codegen prior to real use.
986 os << llvm::formatv(structuredOpIndexingMapsFormat, className,986 os << llvm::formatv(structuredOpIndexingMapsFormat, className,
987- dimIdentsStr, interleaveToString(stmts, "\n "));987+ interleaveToString(stmts, "\n "));
988 }988 }
989 } else {989 } else {
990 os << llvm::formatv(rankPolyStructuredOpIndexingMapsFormat, className);990 os << llvm::formatv(rankPolyStructuredOpIndexingMapsFormat, className);