Introduction to load elimination in the GVN pass

llvm pass系列 在LLVM中,一致性分析框架并不是一个特定的、官方命名的组件,但我们可以理解为在LLVM的架构和工具集中,存在一系列用于确保代码转换和优化过程中一致性的机制和方法。可能除了ir和pass之外,对编译器语义不变这个核心的理论,也值得参考学习,面试的时候最多也就问你知道哪些pass,几乎不会问这些问题,但是这是编译器原理,是编译器的目标约束。它们基于分析pass提供的信息,对程序进行实际的转换和优化操作。LLVM的pass架构使得相同的优化和分析可以在不同的目标架构和操作系统上运行,提高了代码的跨平台兼容性。 阅读详情

Thursday, December 17, 2009

Introduction to load elimination in the GVN pass

One very important optimization that the GVN pass (opt -gvn) does is load elimination. Load elimination involves several subsystems (including alias analysis, memory dependence analysis, SSA construction, PHI translation) and has many facets (full vs partial redundancy elimination, value coercion, handling memset/memcpy, etc). In this post, I introduce and motivate the topic, which will let us expand on it in future posts.

Basic Redundant Load Elimination

GVN (which stands for “Global Value Numbering”, though this detail has nothing to do with load elimination) is responsible for the form of load elimination known as redundant load elimination. This is the elimination of loads whose values are already available (other forms of load elimination includes deletion of dead loads). A simple example of a redundant load is:

  %x = load i32* %P
  %y = load i32* %P  ; <- Redundant

Clearly in this example, we can delete the second load and “replace all uses” of %y with %x, because the value at address %p is already available as the value “i32 %x”. Another simple form of redundant elimination comes about from stores, as in:

  store i32 4, i32* %P
  %a = load i32* %P

In this example, we can replace %a with 4. This sort of transformation generalizes to support other operations: the GVN pass can forward memsets to loads, memcpy from constant globals to loads, etc. Note that GVN is not allowed to optimize away volatile loads.

The implementation of this is pretty straight-forward: GVN (through the memdep class) scans backwards from the load that we are trying to eliminate up through the block until it gets an instruction that provides the value (as in these examples) or until it finds an instruction that might affect the memory in an unknown way like a call. If we find an instruction that potentially clobbers the memory location, we can’t eliminate the load.

However, straight-line code is pretty boring, lets look at more complex examples.

SSA Construction in GVN

GVN can also eliminate non-local loads, which can require PHI node insertion. Here’s a simple example:

BB1:
  store i32 5, i32* %P
  br label %Merge

BB2:
  %X = load i32* %P
   ... use %X ...
   br label %Merge

Merge:
  %Y = load i32 *%P
  ...
  ... use %Y ...

In this case, GVN scans for available values of %P within the block (starting at the %Y load), and runs into the top of the %Merge block. Since it got to the top of the block, it starts scanning the predecessor blocks (%BB1 and %BB2) and it finds out that the value of the load is 5 in BB1 and %X in BB2. GVN then renames these values with the SSAUpdater class, producing:

BB1:
  store i32 5, i32* %P
  br label %Merge

BB2:
  %X = load i32* %P
  ... use %X ...
  br label %Merge

Merge:
  %Y = phi i32 [5, %BB1], [%X, %BB2]
  ...
  ... use %Y ...

Replacing a load with a PHI node may not seem like a win, however the cost model we use in the LLVM optimizer assumes that PHI nodes will be coalesced away by the code generator, and are thus free. The logic that does PHI insertion is contained and maintained by the SSAUpdater class, which may be the subject of a future Blog post.

Pros and Cons of Redundant Load Elimination

We always consider it profitable to eliminate a load in the optimizer when possible. Loads can be quite expensive (e.g. if they miss in the cache), and load/store traffic can hide other redundant or further simplifyable logic from the scalar optimizer. One idiom used to test alias analyses usually looks like this:

  %A = load i32* %P
  store i32 1, i32* %Q
  %B = load i32* %P

  %C = sub i32 %A, %B
  ret i32 %C

If GVN + instcombine are able to turn this into “ret i32 0” then we know that alias analysis was able to prove that P and Q did not alias. While somewhat unlikely in the real world, this is one example that shows a scalar optimization (X-X == 0) that can not be done unless the redundant loads are eliminated.

The cost of eliminating redundant loads is that it creates longer live ranges that the code generator may not be able to cope with. This is a real issue that we don’t currently have a good solution for. The ultimate answer is that improved rematerialization will be able to rematerialize the load further down in the code to reduce register pressure, we don’t have great support for this yet though.

LLVM数据流分析的理论 LLVM数据流分析的理论 标量优化(scalar目录): 死代码消除(BDCE.cpp[code],ADCE.cpp[code],DCE.cpp[code]), 全局值编号(GVN.cpp[code]), 代码提升(ConstantHoisting.cpp[code]), 公共子表达式消除(EarlyCSE.cpp[code]), 代码下沉(Sink.cpp[code]), 以及各种循环优化等 过程间优化(IPO目录):无效参数消除(DeadArgumentElimination.cpp[code]) , 阅读详情

相关推荐

三分钟解决RKNN-Toolkit2安装失败:从模块报错到成功验证的完整记录

本文详细记录了RKNN-Toolkit2安装失败的解决方案,从环境配置到模型转换实战,特别针对常见的'No module named 'rknn''报错提供了完整修复指南。通过YOLOv8模型转换示例,展示了从ONNX到RKNN格式的完整流程,并分享了在RK3588芯片上的部署优化技巧。

html8的博客 264

LLVM 编译器学习笔记之六十五 -- GVN(global value number)

1、实现方法:GVN算法及LLVM中的实现 - 薛奇星 - 20210217 - PLCT实验室_哔哩哔哩_bilibili

~ 飞 扬 的 天 空 ~ 1849

PCB测试点

PCB测试点

qq_31444421的博客 1万+

LVN和SVN算法在LLVM中的实现?

为了更深入地了解LLVM中LVN和SVN的实现,建议直接查看LLVM的源代码,特别是与GVN相关的部分,以及可能的自定义优化Pass。然而,可以推断出LLVM中的LVN实现可能通过限制GVN的作用域来模拟,而SVN则可能需要自定义优化Pass来实现。LLVM的LVN实现可能隐藏在GVN或类似的优化Pass中,因为LLVM的官方文档和源代码主要强调GVN,它作用于整个控制流图(CFG),但在某些情况下,可以通过限制其作用域来模拟LVN的效果。

怀揣梦想,一颗执着于技术的心从未磨灭,内心住着一颗顽强的小强时刻提醒自己层层突破自我,同时也成就他人 515

HDLbits找茬实战:5个Verilog仿真Bug修复案例,新手避坑指南

本文通过HDLbits平台上的5个典型Verilog仿真Bug案例,详细解析了多路选择器位宽不匹配、模块实例化端口映射错误等常见问题,提供实用的修复方案和调试技巧,帮助Verilog新手快速掌握代码验证与仿真技能,避免常见陷阱。

weixin_42653691的博客 162

天猫精灵设备拆机及系统分析

我手头的这个天猫精灵型号为TG_X1,黑色天猫精灵AI助手,拆解没有多少难度,但需要注意连接线的问题。具体的拆机过程可以参见:http://www.geekifix.com/wp/archives/415.html注意一点就是,天猫精灵的主板的位置,位于音频按键的底部,也就是顶端,而拆解是从电源的位置从低到顶进行拆机;拆机后,看到主控板的信息如下:主控型号:联发科 MT8516MT8516支持四核...

LeeKwen的专栏 3万+

Elimination

完全背包的题目,代码实现并不难,但是要理解题意,英语是硬伤~ 题意(人工翻译): 赢得其中一场淘汰赛的选手将会入围2214年的"Russian Code Cup"比赛 淘汰赛的赛制分为初赛和复赛,每场初赛都有c道问题,初赛的获胜者是排名前n位的选手,每场复赛的都有d道题目,复赛的获胜者只有一名,另外,在过去的决赛获胜的k名选手直接被邀请参加决赛而不用淘汰赛 在所有的的淘汰赛结束之后应该有不

smoggyxhdz的博客 638

1.14、基于NARX神经网络的磁悬浮建模(matlab)

数据准备:首先,需要准备用于训练和测试的磁悬浮系统的输入和输出数据,包括磁力、电流、位置、速度等信息。数据可以被保存在MATLAB的数据文件中,并加载到工作空间中。使用MATLAB的神经网络工具箱,创建一个NARX神经网络。需要定义网络的层次结构、节点数、激活函数等参数。通常,NARX网络包括自回归层和外部输入层。数据预处理:对加载的数据进行预处理,包括去除噪声、归一化处理等操作,以确保数据质量和网络训练的稳定性。使用MATLAB的训练函数,如trainlm或者trainbr,对NARX神经网络进行训练。

XU157303764的博客 1999

CodeForces - 417A Elimination (完全背包)

题目链接:http://codeforces.com/problemset/problem/417/A点击打开链接 A. Elimination time limit per test 1 second memory limit per test 256 megabytes input standard input output stand

xuejye的博客 511

基于AOD-Net与GAN的深度学习去雾算法开发

本文提出一种结合AOD-Net与GAN的深度学习去雾算法。通过改进的AOD-GAN网络架构,集成风格迁移模块,设计包含对抗损失、内容损失和风格损失的复合目标函数。实验表明,该方法在PSNR(24.67)、SSIM(0.92)和FID(32.15)指标上优于传统方法,实现了端到端的物理模型引导图像去雾。未来将优化多尺度特征融合和轻量化部署。

huanghm88的专栏 594

127.手机防回滚机制(Anti-Rollback)详解|efuse熔丝原理与降级绕过方案

手机刷机与维修技术指南 本文针对电子工程师和维修人员,详细介绍了主流手机品牌(高通、联发科、苹果)的刷机原理与维修方法。内容包括三大平台的刷机协议差异、关键操作模式(Fastboot/DFU/EDL)、分区表修复等核心技术,并提供了完整的Python自动化刷机脚本。文章重点解决系统崩溃、基带丢失、分区损坏等常见故障,涵盖从环境搭建到具体操作的全流程指导,所有指令均经过实测验证。适用于专业维修场景下的跨品牌设备修复需求。

2601_95651806的博客 294

LLVM编译流程详解

LLVM 什么是LLVM? LLVM项目是一系列分模块、可重用的编译工具链。它提供了一种代码编写良好的中间表示(IR),可以作为多种语言的后端,还可以提供与变成语言无关的优化和针对多种cpu的代码生成功能。 传统编译器分为三个阶段:前端—>优化器—>后端 LLVM也分为三个阶段,但是设计上有区别,LLVM不同的就是对于不同的语言他都提供了同一种中间表示: 前端可以使用不同的编译工具对代码文件做词法分析以形成抽象语法树AST,然后将分析好的代码转换成LLVM的中间表示..

就是这个七昂的博客 1万+

荣耀Magic 6 dToF LiDAR Android阵营全球第一首发

40X30分辨率的BSI SPAD小面阵,帧率达到60帧,搭配多传感器融合算法, 可产生足够的稠密点云,为应用设备提供可靠的距离传感。在手机消费电子方案中,dToF LiDAR技术不仅提升了手机的功能和用户体验,还为手机厂商提供了差异化的竞争优势。随着技术的不断进步和市场的快速发展,我们可以期待未来dToF LiDAR技术将在手机领域发挥更大的作用,为用户带来更多创新和便捷的功能。增强现实(AR)体验:通过dToF LiDAR提供的高精度深度信息,手机可以更准确地放置虚拟物体在现实世界的表面上。

致力于lidar应用开发 19万+

LLVM的分析和转换Passes

LLVM的分析和转换Passes1 简介2 分析Passes2.1 -aa-eval:详细的别名分析精确评估2.2 -basicaa:基本别名分析(无状态别名分析实现)2.3 -basiccg:基本的CallGraph结构2.4 -count-aa:统计别名分析查询响应2.5 -da:依赖关系分析2.6 -debug-AA:使用debugger的别名分析2.7 -domfrontier:支配边界构...

梦在哪里的博客 5602

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

codeforces 417A A. Elimination

A. Elimination time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The finalists of the "Russian Code Cup" compe

yjt9299的博客 349

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Java循环不变表达式外提_LLVM的分析和转换Passes

LLVM的分析和转换Passes1 简介2 分析Passes2.1 -aa-eval:详细的别名分析精确评估2.2 -basicaa:基本别名分析(无状态别名分析实现)2.3 -basiccg:基本的CallGraph结构2.4 -count-aa:统计别名分析查询响应2.5 -da:依赖关系分析2.6 -debug-AA:使用debugger的别名分析2.7 -domfrontier:支配边界构...

weixin_39637979的博客 1015

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Advanced Topics in Redundant Load Elimination with a Focus on PHI Translation

Saturday, December 19, 2009 Advanced Topics in Redundant Load Elimination with a Focus on PHI Translation In our previous post on GVN we introduced some basics of load elimination. This post describe...

ronnie88597的博客 883

LLVM全时优化

引子由于实验室项目原因,从本科开始接触Clang和LLVM开始到现在已经有2年时间了,期间都是针对Clang做一些边边角角的工作,没有潜下心来好好研读一下LLVM的paper。最近闲下来读了Chris Lattner那篇引用率高达2227的论文《LLVM: A Compilation Framework for Lifelong Program Analysis & Transformation》,

电影旅行敲代码 9569

CodeForces - To Crash Or Not To Crash

题目链接:http://codeforces.com/gym/102219/problem/ITime limit per test 1.0 sMemory limit per test 256 MB Description Ethics regarding artificial intelligence (AI) is an important topic at current times...

ityanger的技术栈 3610

Codeforces 417A Elimination(水题)

题目链接:Codeforces 417A Elimination 题目大意:

不慌不忙、不急不躁 1523

Writing an LLVM Pass

目录 1.简介 - 什么是pass? 2.快速入门 - 写下hello world 3.设置构建环境 4.Basic code required 5.Pass类和要求 6.ImmutablePass类 7.ModulePass类 8.CallGraphSCCPass类 9.FunctionPass类 10.doInitialization(Module&)方法 11.L...

zhang14916的博客 2705

codeforces 859A Declined Finalists

A. Declined Finalists time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output This year, as in previous years, MemS

shyoldman的博客 873

The '08 Open Source CMS Awards Finalists Are In_longware_新浪博客

PS:这是老外的,国内行情不一,参考一下波~ Packt Publishing刚刚宣布2008届最佳开源CMS大赛各奖项下的决赛名单。去年,Drupal是综合大奖得主,将06年冠军 Joomla!推到第二位。今年 Drupal 和 Joomla 均入选决赛名单,分属不同奖项。Packt 将于2008年10月31日开始公布最终获奖名单。 开源CMS综合大奖决赛名单 Dot...

三V工作室·longware 238

GVARUSL--上海

一个字就是干净 线条 前景 正中间 又是这个技巧,人处于两条线之间 有掩体 线条也很完美 三角形

weixin_42907150的博客 273

LLVM学习之OPT工具

1.概述 ELF文件是Linux中的目标文件。 分为: 可执行文件 可重定位文件(.o) 共享目标文件(.so)

weixin_42492218的博客 6043
上一篇: Intro to the LLVM MC Project
下一篇: libc++ and C++1Y
「已注销」
博客等级 码龄11年 2粉丝 18原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值