news 2026/6/13 9:00:01

MLIR专题10:下译LLVM IR

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
MLIR专题10:下译LLVM IR

我们还是以toy例子为实例,完整的代码如下:

//===----------------------------------------------------------------------===// // ToyToLLVM RewritePatterns //===----------------------------------------------------------------------===// namespace { /// Lowers `toy.print` to a loop nest calling `printf` on each of the individual /// elements of the array. class PrintOpLowering : public ConversionPattern { public: explicit PrintOpLowering(MLIRContext *context) : ConversionPattern(toy::PrintOp::getOperationName(), 1, context) {} LogicalResult matchAndRewrite(Operation *op, ArrayRef<Value> operands, ConversionPatternRewriter &rewriter) const override { auto *context = rewriter.getContext(); auto memRefType = llvm::cast<MemRefType>((*op->operand_type_begin())); auto memRefShape = memRefType.getShape(); auto loc = op->getLoc(); ModuleOp parentModule = op->getParentOfType<ModuleOp>(); // Get a symbol reference to the printf function, inserting it if necessary. auto printfRef = getOrInsertPrintf(rewriter, parentModule); Value formatSpecifierCst = getOrCreateGlobalString( loc, rewriter, "frmt_spec", StringRef("%f \0", 4), parentModule); Value newLineCst = getOrCreateGlobalString( loc, rewriter, "nl", StringRef("\n\0", 2), parentModule); // Create a loop for each of the dimensions within the shape. SmallVector<Value, 4> loopIvs; for (unsigned i = 0, e = memRefShape.size(); i != e; ++i) { auto lowerBound = rewriter.create<arith::ConstantIndexOp>(loc, 0); auto upperBound = rewriter.create<arith::ConstantIndexOp>(loc, memRefShape[i]); auto step = rewriter.create<arith::ConstantIndexOp>(loc, 1); auto loop = rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step); for (Operation &nested : make_early_inc_range(*loop.getBody())) rewriter.eraseOp(&nested); loopIvs.push_back(loop.getInductionVar()); // Terminate the loop body. rewriter.setInsertionPointToEnd(loop.getBody()); // Insert a newline after each of the inner dimensions of the shape. if (i != e - 1) rewriter.create<LLVM::CallOp>(loc, getPrintfType(context), printfRef, newLineCst); rewriter.create<scf::YieldOp>(loc); rewriter.setInsertionPointToStart(loop.getBody()); } // Generate a call to printf for the current element of the loop. auto printOp = cast<toy::PrintOp>(op); auto elementLoad = rewriter.create<memref::LoadOp>(loc, printOp.getInput(), loopIvs); rewriter.create<LLVM::CallOp>( loc, getPrintfType(context), printfRef, ArrayRef<Value>({formatSpecifierCst, elementLoad})); // Notify the rewriter that this operation has been removed. rewriter.eraseOp(op); return success(); } private: /// Create a function declaration for printf, the signature is: /// * `i32 (i8*, ...)` static LLVM::LLVMFunctionType getPrintfType(MLIRContext *context) { auto llvmI32Ty = IntegerType::get(context, 32); auto llvmPtrTy = LLVM::LLVMPointerType::get(context); auto llvmFnType = LLVM::LLVMFunctionType::get(llvmI32Ty, llvmPtrTy, /*isVarArg=*/true); return llvmFnType; } /// Return a symbol reference to the printf function, inserting it into the /// module if necessary. static FlatSymbolRefAttr getOrInsertPrintf(PatternRewriter &rewriter, ModuleOp module) { auto *context = module.getContext(); if (module.lookupSymbol<LLVM::LLVMFuncOp>("pr
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/13 8:57:02

ArcGIS Pro插件开发避坑:多线程操作UI时,进度框更新卡顿怎么办?

ArcGIS Pro插件开发实战&#xff1a;多线程环境下高效更新UI进度框的工程化解决方案 当你在ArcGIS Pro中开发需要执行长时间地理处理任务的插件时&#xff0c;一个流畅的进度反馈系统不仅能提升用户体验&#xff0c;更是调试优化的重要工具。但许多开发者都会遇到这样的困境&am…

作者头像 李华
网站建设 2026/6/13 8:57:01

Keras实现多语种神经机器翻译的工业级实践

1. 项目概述&#xff1a;为什么“多语种神经机器翻译”不是简单堆叠几个模型“多语种神经机器翻译”这个标题里&#xff0c;“多语种”三个字最容易被误解——很多人第一反应是“我先训练一个中英模型&#xff0c;再训一个中日模型&#xff0c;最后打包成一个工具”&#xff0c…

作者头像 李华
网站建设 2026/6/13 8:55:57

Unity游戏多语言智能翻译引擎:XUnity.AutoTranslator技术架构深度解析

Unity游戏多语言智能翻译引擎&#xff1a;XUnity.AutoTranslator技术架构深度解析 【免费下载链接】XUnity.AutoTranslator 项目地址: https://gitcode.com/gh_mirrors/xu/XUnity.AutoTranslator 在全球化游戏市场中&#xff0c;语言障碍成为玩家体验海外优质作品的主要…

作者头像 李华
网站建设 2026/6/13 8:54:09

上下文工程实战:6种模式构建高可靠RAG问答系统

1. 项目概述&#xff1a;这不是调提示词&#xff0c;是重构问答系统的“神经突触”“Context Engineering”这个词最近在大模型应用圈里被反复提起&#xff0c;但很多人一听到就下意识点开ChatGPT&#xff0c;敲几行“请用专业术语回答”“请分三点说明”&#xff0c;然后截图发…

作者头像 李华
网站建设 2026/6/13 8:39:54

C#工业通讯DLL:支持Modbus TCP全功能指令与REAL/DINT数据读写

本文还有配套的精品资源&#xff0c;点击获取 简介&#xff1a;直接集成到C# WinForm项目的Modbus TCP通信组件&#xff0c;封装为轻量级DLL文件&#xff0c;无需安装额外运行时或驱动。支持标准功能码01&#xff08;读线圈&#xff09;、02&#xff08;读离散输入&#xff…

作者头像 李华