当前位置: 首页 > news >正文

NonDefUseDependency及例子

在研究ReassociatePass的优化时,我发现了其中有一个函数bool llvm::mayHaveNonDefUseDependency(const Instruction &I) {,这个函数会判断一个指令是否是不可优化的,也即是否是可能会造成NonDefUseDependency的。
潜在的非定义使用依赖关系指的是在程序中,某个指令使用了另一个指令产生的值,但两者之间的关系不是经过明确定义的。这可能导致程序在并行执行或重排序时出现问题,因为执行顺序的改变可能会影响到数据的正确性。
这种所谓的没有经过明确定义的,也即没有通过限制指令标记两个指令之间是不可移动或不可并行的,但两个指令之间有明显的先后关系,如果并行可能会出现某种失败的情况。

一个例子

#include <iostream>
#include <omp.h>int main() {int x = 0;#pragma omp parallel{#pragma omp forfor(int i = 0; i < 100; ++i)x = x + 1;}std::cout << x << std::endl;
}

omp.h头文件主要是将#pragma omp编译为openmp代码。运行指令如下:

clang -fopenmp source -o target

mayHaveNonDefUseDependency

bool llvm::mayHaveNonDefUseDependency(const Instruction &I) {if (I.mayReadOrWriteMemory())// Memory dependency possiblereturn true;if (!isSafeToSpeculativelyExecute(&I))// Can't move above a maythrow call or infinite loop.  Or if an// inalloca alloca, above a stacksave call.return true;if (!isGuaranteedToTransferExecutionToSuccessor(&I))// 1) Can't reorder two inf-loop calls, even if readonly// 2) Also can't reorder an inf-loop call below a instruction which isn't//    safe to speculative execute.  (Inverse of above)return true;return false;
}
  1. 首先判断是否读写内存,常见的例如Load Store指令,Call, Invoke指令。
  2. 判断是否是safeToSpeculativelyExecute,此处指的是不能安全的推断执行的指令。能够推测执行的指令,例如如果除数为0,则会返回false,也即mayHaveNonDefUseDependency会返回true.
  3. 判断GuaranteedtoTransferExecutionToSuccessor,该函数用于判断一个指令是否可以将控制流转移到指令后。如果是Return指令或不可达指令,显然会返回false。如果是CatchPad指令,需要判断。最后判断指令是否mayThrow或者willReturn,如果不满足mayThro且willReturn,就会返回true.
bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) {// Note: An atomic operation isn't guaranteed to return in a reasonable amount// of time because it's possible for another thread to interfere with it for an// arbitrary length of time, but programs aren't allowed to rely on that.// If there is no successor, then execution can't transfer to it.if (isa<ReturnInst>(I))return false;if (isa<UnreachableInst>(I))return false;// Note: Do not add new checks here; instead, change Instruction::mayThrow or// Instruction::willReturn.//// FIXME: Move this check into Instruction::willReturn.if (isa<CatchPadInst>(I)) { switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {default:// A catchpad may invoke exception object constructors and such, which// in some languages can be arbitrary code, so be conservative by default.return false;case EHPersonality::CoreCLR:// For CoreCLR, it just involves a type test.return true;}}// An instruction that returns without throwing must transfer control flow// to a successor.return !I->mayThrow() && I->willReturn();
}

我编译的clang暂时不支持openmp,故此处不测试。

相关文章:

  • 《go语言实战》笔记第三章-go doc(文档)
  • 论文阅读-MapReduce
  • Netty源码三:NioEventLoop创建与run方法
  • Linux ---- Shell编程之正则表达式
  • Java 的 Map 與 List
  • linux新增用户,指定home目录和bash脚本且加入到sudoer列表
  • 从0开始搭建若依微服务项目 RuoYi-Cloud(保姆式教程完结)
  • css3表格练习
  • 不同的强化学习模型适配与金融二级市场的功能性建议
  • 爬虫学习笔记-selenium交互
  • HttpHeaders 源码中headers成员变量为什么声明为final
  • Wireshark网络协议分析 - Wireshark速览
  • 详解SpringCloud微服务技术栈:深入ElasticSearch(1)——数据聚合
  • 设计模式篇---备忘录模式
  • uniapp微信小程序-请求二次封装(直接可用)
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 【347天】每日项目总结系列085(2018.01.18)
  • MQ框架的比较
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • tweak 支持第三方库
  • 我建了一个叫Hello World的项目
  • const的用法,特别是用在函数前面与后面的区别
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • ​软考-高级-系统架构设计师教程(清华第2版)【第1章-绪论-思维导图】​
  • #我与虚拟机的故事#连载20:周志明虚拟机第 3 版:到底值不值得买?
  • (3)(3.5) 遥测无线电区域条例
  • (第一天)包装对象、作用域、创建对象
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (六)激光线扫描-三维重建
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (四)【Jmeter】 JMeter的界面布局与组件概述
  • (图)IntelliTrace Tools 跟踪云端程序
  • (一一四)第九章编程练习
  • (原創) 如何刪除Windows Live Writer留在本機的文章? (Web) (Windows Live Writer)
  • **python多态
  • .NET/C# 在 64 位进程中读取 32 位进程重定向后的注册表
  • .NetCore Flurl.Http 升级到4.0后 https 无法建立SSL连接
  • .Net高阶异常处理第二篇~~ dump进阶之MiniDumpWriter
  • .Net接口调试与案例
  • /*在DataTable中更新、删除数据*/
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • @拔赤:Web前端开发十日谈
  • [ SNOI 2013 ] Quare
  • [ 攻防演练演示篇 ] 利用通达OA 文件上传漏洞上传webshell获取主机权限
  • [20170713] 无法访问SQL Server
  • [autojs]autojs开关按钮的简单使用
  • [BZOJ 2142]礼物(扩展Lucas定理)
  • [bzoj1038][ZJOI2008]瞭望塔
  • [C#][DevPress]事件委托的使用
  • [C#]OpenCvSharp使用帧差法或者三帧差法检测移动物体
  • [c++] 自写 MyString 类
  • [C++]unordered系列关联式容器
  • [C++11 多线程同步] --- 条件变量的那些坑【条件变量信号丢失和条件变量虚假唤醒(spurious wakeup)】
  • [codevs 2822] 爱在心中 【tarjan 算法】
  • [CSS]浮动