Instruction Relationship Framework in LLVM

LLVM 编译器学习笔记之三十六-- 指令调度Instruction scheduling 1、参考Instruction scheduling in LLVM - 知乎,在中后端均存在指令调度 指令调度模型 2、在llvm12->llvm14 中Machine Instruction Scheduler有个patch增强了load/store指令的合并,也就是指令调度不仅仅只是改变指令的顺序,该优化类似gcc中的store-merge优化 ..................................................................... 阅读详情

Instruction Relationship Framework in LLVM

The article provides an overview of the new Relationship framework of TableGen. This TableGen feature is used to describe user defined relationships between instructions. It was added to LLVM in October 2012.

Motivation:

The motivation for this feature stemmed from the Hexagon backend. Much like other processors, Hexagon provides multiple variations for many instructions. It is a common requirement in machine instruction passes to switch between various formats of the same instruction. For example, consider an Add instruction with predicated true (Add_pt) and predicated false (Add_pf) forms. Let’s assume that a non-predicated Add instruction is selected during target lowering. However, during if-conversion, the optimization pass might decide to change the non-predicated Add into the predicated true Add_pt form. These transformations require a framework to relate non-predicated forms to the respective predicated forms. In the absence of such a framework, this transformation is typically achieved using large switch cases. There are many deficiencies in using a switch case based approach. The manual implementation of switch case clauses requires a very high maintenance cost. It also results in lost optimization opportunities due to incomplete implementation of switch cases. The lack of a relationship model resulted in around 15% of Hexagon backend code dedicated to switch cases with several of those functions growing to over thousands of lines of code.

This problem inspired us to explore some alternatives. We started to look for a framework that was easy to maintain, flexible, scalable, and less error prone. After some initial discussions and brainstorming in the Hexagon group, we decided to modify TableGen to express instruction relations. The initial design was submitted to the LLVM-dev mailing list for review. Jakob Stoklund gave valuable suggestions and helped with the design of the relationship framework. The idea was to add a query language that can be used to define different kind of relationships between instructions. TableGen was extended to parse relationship models. It uses the information to construct tables which are queried to determine new opcodes corresponding to a relationship. The Hexagon backend relies heavily on the relationship framework and it has significantly improved the code quality of our target.

Before getting into the implementation details, let’s consider an API that takes an instruction opcode as input and returns its predicated true/false form. We’ll first look at the switch-case based solution and then compare it with the relationship based implementation.

short getPredicatedTrue(short opcode) {
switch (opcode) {
default:
  return -1;
case Hexagon::Add:
  return Hexagon::Add_pt;
case Hexagon::Sub:
  return Hexagon::Sub_pt;
case Hexagon::And:
  return Hexagon::And_pt;
case Hexagon::Or:
  return Hexagon::Or_pt;
case ... :
  return ...
}

short getPredicatedFalse(short opcode) {
switch (opcode) {
default:
  return -1;
case Hexagon::Add:
  return Hexagon::Add_pf;
case Hexagon::Sub:
  return Hexagon::Sub_pf;
case Hexagon::And:
  return Hexagon::And_pf;
case Hexagon::Or:
  return Hexagon::Or_pf;
case ... :
  return ...
}

short getPredicatedOpcode(short opcode, bool predSense) {
return predSense ? getPredicatedTrue(opcode)
                 : getPredicatedFlase(opcode);
}

The switch-case based approach becomes quite unwieldy because of the large number of cases. Also, it requires continuous maintenance as new instructions are added. The problem becomes more demanding when an instruction has multiple relations since each of these APIs must be updated.

The relationship framework offers a very systematic solution to this problem. It requires instructions to model their attributes and categorize their groups. For instance, a field called ‘PredSense’ can be used to record whether an instruction is predicated or not and its sense of predication. Each instruction in the group has to be unique such that no two instructions can share all the same attributes. There must be at least one field with different value. The instruction groups are modeled by assigning a common base name to all the instructions in the group. One of the biggest advantages of this approach is that, by modeling these attributes and groups once, we can define multiple relationships with very little effort.

With the relationship framework, the getPredicatedOpcode API can be implemented as below:

short getPredicatedOpcode(short opcode, bool predSense) {
return predSense ? getPredicated(opcode, PredSenseTrue)
                 : getPredicated(opcode, PredSenseFalse);
}

Here, getPredicated() function is automatically generated by the relationship framework. It performs a query into the corresponding relationship table, also generated by the framework, and returns the matching predicated opcode if found.

Architecture:

The entire framework is driven by a class called InstrMapping. The TableGen back-end has been extended to include a new parser for the relationship models implemented using InstrMapping class. Any relationship model must derive from this class and assign all the class members to the appropriate values. This is how the class looks like:

class InstrMapping {
  // Used to reduce search space only to the instructions using this relation model
  string FilterClass;

  // List of fields/attributes that should be same for all the instructions in
  // a row of the relation table. Think of this as a set of properties shared
  // by all the instructions related by this relationship.
  list RowFields = [];
  // List of fields/attributes that are same for all the instructions
  // in a column of the relation table.
  list ColFields = [];

  // Values for the fields/attributes listed in 'ColFields' corresponding to
  // the key instruction. This is the instruction that will be transformed
  // using this relation model.
  list KeyCol = [];

  // List of values for the fields/attributes listed in 'ColFields', one for
  // each column in the relation table. These are the instructions a key
  // instruction will be transformed into.
  list > ValueCols = [];
}

Now, let’s revisit the getPredicated API. As mentioned earlier, this function can be auto-generated by the relationship framework. It requires us to define a relationship model that can relate non-predicated instructions with their predicated forms:

def getPredicated : InstrMapping {
  // Choose a FilterClass that is used as a base class for all the instructions modeling
  // this relationship. This is done to reduce the search space only to these set of instructions.
  let FilterClass = "PredRel";

  // Instructions with same values for all the fields in RowFields form a row in the resulting
  // relation table.
  // For example, if we want to relate 'Add' (non-predicated) with 'Add_pt'
  // (predicated true) and 'Add_pf' (predicated false), then all 3
  // instructions need to have a common base name, i.e., same value for BaseOpcode here. It can be 
  // any unique value (Ex: XYZ) and should not be shared with any other instruction not related to 'Add'.
  let RowFields = ["BaseOpcode"];

  // List of attributes that can be used to define key and column instructions for a relation.
  // Here, key instruction is passed as an argument to the function used for querying relation tables.
  // Column instructions are the instructions they (key) can transform into.
  //
  // Here, we choose 'PredSense' as ColFields since this is the unique attribute of the key
  // (non-predicated) and column (true/false) instructions involved in this relationship model.
  let ColFields = ["PredSense"];

  // The key column contains non-predicated instructions.
  let KeyCol = ["none"];

  // Two value columns - first column contains instructions with PredSense=true while the second
  // column has instructions with PredSense=false.
  let ValueCols = [["true"], ["false"]];
}

This relationship model is processed and the information is used to construct a table along with the API to query. All this is emitted in the XXXInstrInfo.inc file. However, with the changes made so far, we may end up with an empty relation table since we haven’t defined PredSense and BaseOpcode for any of the instructions yet.

multiclass ALU32_Pred {
  let PredSense = !if(PredNot, "false", "true"), isPredicated = 1 in
  def NAME : ALU32_rr<(outs IntRegs:$dst),
            (ins PredRegs:$src1, IntRegs:$src2, IntRegs: $src3),
            !if(PredNot, "if (!$src1)", "if ($src1)")#
            " $dst = "#mnemonic#"($src2, $src3)",
            []>;
}

multiclass ALU32_base {
  let BaseOpcode = BaseOp in {
    let isPredicable = 1 in
    def NAME : ALU32_rr<(outs IntRegs:$dst),
            (ins IntRegs:$src1, IntRegs:$src2),
            "$dst = "#mnemonic#"($src1, $src2)",
            [(set (i32 IntRegs:$dst), (OpNode (i32 IntRegs:$src1),
                                              (i32 IntRegs:$src2)))]>;

      defm pt : ALU32_Pred; // Predicate true
      defm pf : ALU32_Pred; // Predicate false
  }
}

let isCommutable = 1 in {
  defm Add : ALU32_base<"add", "ADD", add>, PredRel;
  defm And : ALU32_base<"and", "AND", and>, PredRel;
  defm Xor: ALU32_base<"xor", "XOR", xor>, PredRel;
  defm Or  : ALU32_base<"or", "OR", or>, PredRel;
}
defm Sub : ALU32_base<"sub", "SUB", sub>, PredRel;

Fields highlighted in blue are solely for the purpose of Relationship framework. Here, PredRel is a filter class used to extract instructions that may be related using getPredicated relationship model. All the instructions using this model are expected to derive from PreRel class. BaseOpcode is used to group related instructions together. In the above example, all the variants of Add instruction, Add, Add_pt, Add_pf will have their BaseOpcode set to ADD. Similarly, BaseOpcode for all the variants for Sub is set to SUB. It can be any string unique across all groups. PredSense is used to identify instructions within each group.

With the help of this extra information, TableGen is able to construct the following API. It offers the same functionality as switch-case based approach and significantly reduces the maintenance overhead:

int getPredicated(uint16_t Opcode, enum PredSense inPredSense) {
static const uint16_t getPredicatedTable[][3] = {
  { Hexagon::Add, Hexagon::Add_pt, Hexagon::Add_pf },
  { Hexagon::And, Hexagon::And_pt, Hexagon::And_pf },
  { Hexagon::Or, Hexagon::Or_pt, Hexagon::Or_pf },
  { Hexagon::Sub, Hexagon::Sub_pt, Hexagon::Sub_pf },
  { Hexagon::Xor, Hexagon::Xor_pt, Hexagon::Xor_pf },
}; // End of getPredicatedTable

  unsigned mid;
  unsigned start = 0;
  unsigned end = 5;
  while (start < end) {
    mid = start + (end - start)/2;
    if (Opcode == getPredicatedTable[mid][0]) {
      break;
    }
    if (Opcode < getPredicatedTable[mid][0])
      end = mid;
    else
      start = mid + 1;
  }
  if (start == end)
    return -1; // Instruction doesn't exist in this table.

  if (inPredSense == PredSense_true)
    return getPredOpcodeTable[mid][1];
  if (inPredSense == PredSense_false)
    return getPredicatedTable[mid][2];
  return -1;
}

Once instructions have been defined to appropriately model their properties, defining new instruction mappings become extremely easy. Now, say we want to have an API that allows us to transform a predicate-true instruction into its predicate-false form. This can be done by defining a new relationship model. For this model, we don’t have to modify any of the instruction definitions as they already have all the necessary information present.

//===------------------------------------------------------------------===//
// Generate mapping table to relate predicate-true instructions with their
// predicate-false forms
//
def getFalsePredOpcode : InstrMapping {
  let FilterClass = "PredRel";
  let RowFields = ["BaseOpcode"];
  let ColFields = ["PredSense"];
  let KeyCol = ["true"];
  let ValueCols = [["false"]];
}

Conclusion:

I hope this article succeeds in providing some useful information about the framework. The Hexagon backend makes extensive use of this feature and can be used as a reference for getting started on relationship framework.

Posted by Unknown at 1:42 PM

Labels: codegen, MC

ESP32学习笔记06-UART 的485 通讯模式 通信方式选择UART 控制器支持多种通信模式,使用函数 uart_set_mode() 可以选择模式。选择特定模式后,UART 驱动程序将处理已连接 UART 设备的相应行为。例如,主机开始向 Tx FIFO 缓冲区写入数据时,UART 驱动程序会自动置位 RTS 管脚(逻辑 1);最后一位数据传输完成后,驱动程序就会取消置位 RTS 管脚(逻辑 0)。要使用此模式,软件。 阅读详情

相关推荐

使用循环神经网络预测温度

简介 本文以天气时间序列数据集为例,讨论如何使用深度学习模型预测未来天气温度。 使用的是由德国耶拿马克思普朗克生物地球化学研究所的气象站记录。 数据每10分钟记录一次,共记录14种不同的量,如气温、气压、湿度、风向等,原始数据可追溯到2003年,本例只使用2009-2016年的数据。 数据 下载并解压数据略。 加载数据: data_dir = r'E:\practice\tf2\temperature' fname = os.path.join(data_dir, 'jena_climate_2009_

guotianqing的博客 5285

ARM编译遇到"thumb conditional instruction should be in IT block"问题

问题描述在TK1上编译C++程序,遇到如下问题。 Error: thumb conditional instruction should be in IT block -- `strexeq r1,r2,[r4]'解决方法在CMAKE配置中的参数CMAKE_CXX_FLAGS_RELEASE 后添加”-Wa,-mimplicit-it=thumb”,依然存在问题可考虑在CMAKE_CXX_FLAG

MaybePossible 5899

形位公差标注的常见误区与优化策略

本文深入解析机械设计中形位公差标注的常见误区与优化策略,特别针对NX2406用户提供实用技巧。从标注冗余、基准选择到公差值设定等五大误区入手,结合工程图实例展示如何提升标注精度与效率,帮助工程师避免常见错误,优化设计流程,确保零件功能实现与制造可行性。

info6的博客 247

context_gcc.s:110: Error: thumb conditional instruction should be in IT block `vstmdbeq r1!,{d8-d15}

RT-Thread 移植过程中,编译 libcpu/arm/cortex-m4/context_gcc.s (RT-Thread 启动文件)时出现如下错误: rt-thread/libcpu/arm/cortex-m4/context_gcc.s:110: Error: thumb conditional instruction should be in IT block – vstmdbeq ...

mazhongguang的博客 4022

OpenLayers源码解析1 Map.js

Map.js 父类:./PluggableMap/PluggableMap 引用:5个模块 ./renderer/Composite.js ./PluggableMap.js ./obj.js ./control.js(控件) ./interaction.js(交互) 主要功能:核心组件,渲染地图 必要参数: view layers target controls和interactions要是未指定,使用默认值 方法:createRenderer()? /** * @module ol/Map */ i

qq_45005996的博客 1901

达梦数据库JDBC连接池断开自动重连设置

一、场景 在网络状况不是非常良好,经常会出现暂时性的拥塞或者断开的情况,而且当我们重启数据库时也会发生类似的情况。所以需要配置中间件的连接池来实现连接测试以及自动重连,通过重新配置连接池,成功解决了这个问题。下面会给出一份数据源配置参数详单和一份推荐的数据源配置项。 二、连接池配置参数 1. initialSize:连接池启动时创建的初始化连接数量 2. maxActive:连接池中可同时连接的最大的连接数 3. maxIdle:连接池中最大的空闲的连接数,超过的空闲连接将被释放,如果设置为负

lilv1989的博客 7238

Keil C 编译器常见警告与错误信息的解决方法

9 ***ERROR 107:ADDESS SPACE OVERFLOW SPACE: DATA SEGMENT: _DATA_GOUP_ LENGTH: 0018H ***ERROR 118: REFERENCE MADE TO ERRONEOUS EXTERNAL SYMBOL: VOLUME MODULE: C:/8051/OSDM.OBJ (OSDM) ADDRESS: 4036H 说明d

学海无涯,回头是岸........ 1万+

电压跟随器在ADC采样电路中的作用分析

电压跟随器在ADC采集系统中扮演关键角色,它能解决分压电路因ADC输入阻抗导致的电压误差问题。通过运放构成的电压跟随器可隔离信号源与负载,保证采样精度,尤其适用于高阻抗传感器或高速采样场景。工程应用中需注意运放带宽、轨对轨输出能力及抗干扰设计。合理使用电压跟随器能显著提升信号完整性,在复杂电子系统中成为不可或缺的"信号桥梁。

硬核科技 664

LLVM后端移植--Tablegen指令描述Instruction类解读

1. string Namespace = ""; //指明指令集系统名称 使用示例: let Namespace = "Mips"; 一般在Class Formats中直接指出,例如: 2. dag OutOperandList; //包含MI def操作数列表的dag。 使用示例: let OutOperandList = outs; 一般在Class Formats中直接指出,例如: outs来自于MipsInst: 赋值也有多种类型 ,主要是...

Vincent的博客 2836

Allegro基本规则设置指导书之Analysis Modes

On-line DRC是需要开启的,否则所有DRC都不起作用。调出Analysis Modes,这个是所有DRC的总开关。默认是关闭的,可以用来检查孔是否打在SMD的pin上的情况。器件超出package keepin的DRC开关。所有间距相关的DRC开关都要打开。Pad和Pad连接的DRC开关。测试点Pad到器件DRC开关。测试点在器件下方的DRC开关。阻焊到Pad和线的DRC开关。阻焊到shape的DRC开关。下面介绍常用的一些开关设置。器件在器件禁布的DRC开关。测试点到器件的DRC开关。

weixin_54787054的博客 3555

十年芯片资深IC设计工程师面经系列(四)SDC约束

set_driving_cell:当我们的设计是模块级(Block level)的design时,我们的设计前面一般还有其他的设计,那么前面模块的输出便是我们模块的输入,5.1set_false_path:可以单独的时钟或者路径进行约束。set drive :当我们的设计是芯片级(Chip level)的顶层设计时,芯片外围电路要驱动的是负载较大的IO单元,故需要的驱动能力较强,:set_load 是指output端口的负载,设置的值会影响输出到端口的net上的电容,从而影响相应的路径延时。

weixin_47349046的博客 1775

【Keil备忘录】Keil MDK Warning: L6989W 数学计算函数警告问题

keil MDK AC5 AC6的通用警告消除方法参考,数学计算库警告:L6989W的消除方法

幻蜗的笔记 1645

以阿克曼转向车为例对URDF和XACRO文件进行解读

   最近我在古月学院购买了在Gazebo中实现阿克曼转向车的仿真课程,课程资料的阿克曼小车的XACRO文件在Gazebo里加载的时候报了三个错误,在这之前,我没接触过URDF或XACRO文件,本篇文章就以这个阿克曼小车的XACRO文件为例,对URDF或XACRO文件的格式和内容进行解读,期望经过解读和学习,找到错误原因及所在,并解决这三个错误    先来放一下上面右图所示的模型的完整的XACRO文件: <?xml version="1.0"?> <!-- racecar.urdf.x

慕羽★的博客 3704

编译器 | 从工业实现到极简设计 —— 编译器的多元形态与实践

……

u013669912的博客 906

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

【Keil】使用Keil5时出现的错误(持续更新)

目录 1、常见编译错误 2、常见逻辑错误 3、常见仿真错误 1、常见编译错误 error C141: `syntax error near 'extern', expected 'hdata'` (错误C141:在“extern”附近有语法错误,应为“hdata”) 这种错误可能的原因有很多,比较常见且难以发现的便是在调用头文件的时候被调用的头文件写的不完善而导致编译器对下一...

永远年轻 永远热泪盈眶 2万+

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

thumb conditional instruction should be in IT block -- `addcs r5,r5,#65536‘

对于 thumb conditional instruction should be in IT block – `addcs r5,r5,#65536’ 错误的解决办法 错误形式 解决办法 在编译 .c文件的语句中加上“-Wa,-mimplicit-it=thumb” 例如我这里是makefile中: 问题解决,希望对大家有帮助 ...

一只青木呀 1481

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

移植RT-Thread编译报错thumb conditional instruction should be in IT block

在"MCUGCCAssembler"Miscellaneous中增加Option“-Wa,-mimplicit-it=thumb”否则在编译context_gcc.S时会报错误。

qq_40979966的博客 669

中英文对照—masm编译时错误性息提示

<br />ml.exe错误性息<br /><br />FATAL  严重错误<br />cannot open file不能打开文件 <br />I/O error closing fileI/O错误 正在关闭文件<br />I/O error writing fileI/O错误 正在写文件<br />I/O error reading file           I/O错误 正在读取文件<br />out of memory             缺少内存<br />assembler limit 

fengbonianshao的专栏 5463

汇编语言程序常见错误整合

汇编语言,虽然不如现在的面向对象语言和面向过程语言一样流行,但是我们说每一门语言都有它独到的优点。汇编语言是直接面向处理器(Processor)的程序设计语言。处理器是在指令的控制下工作的,处理器可以识别的每一条指令称为机器指令。每一种处理器都有自己可以识别的一整套指令,称为指令集。处理器执行指令时,根据不同的指令采取不同的动作,完成不同的功能,既可以改变自己内部的工作状态,也能控制其它外围电路的...

寄居蟹的博客 2万+

MPlayer移植到arm开发板

今天用了一天的时间将mplayer移植到arm开发板上,移植过程中遇到各种问题,在此做以记录。mplayer版本:1.0rc2Ubuntu版本:12.04 64位移植过程:1、su获取root权限;2、解压mplayer;3、在解压的目录中mkdir output,作为安装目录;3、./configure配置。具体如下:./configure --enable-cross-compile \ ...

w_b_h的专栏 3238

llvm libLLVMCore源码分析 05 - Instruction Class

源码路径 llvm\include\llvm\IR\Instruction.h llvm\include\llvm\IR\Instruction.def llvm\include\llvm\IR\Instructions.h llvm\include\llvm\IR\InstrTypes.h Instruction和BasicBlock 在分析代码前,需要先介绍一下llvm IR的组织结构。在llmv中,一个Module中可以有n个Function,Function内可以有n个BasicBloc

weixin_42654107的博客 1325

LLVM每日谈之十八 GEP Instruction的几点总结

In summary, here’s some things to always remember about the GetElementPtr instruction: The GEP instruction never accesses memory, it only provides pointer computations.The first operand to the ...

weixin_30672019的博客 343

永远不要在代码中使用「User」这个单词

点击蓝色“程序猿DD”关注我哟加个“星标”,不忘签到哦来源:21CTO社区当你意识到你在项目开始时做的轻量、简单的设想竟然完全错了时,你已经用了六个月的时间投入到这个项目...

程序猿DD 1145

LLVM IR(五)——IR指令介绍(Instruction Reference)

文章目录5.1 终端指令(Terminator Instructions)ret指令br指令switch指令indirectbr指令5.2 一元运算(Unary Operations)fneg指令5.3 二元运算(Binary Operations)add指令fadd指令sub指令fsub指令sdiv指令fdiv指令urem指令srem指令frem指令5.4 二进制位运算(Bitwise Binary Operations)shl指令lshr指令ashr指令and指令or指令xor指令5.5 向量运算(Ve

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值