news 2026/4/30 14:58:44

LLVM后端入门8:Subtarget支持

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
LLVM后端入门8:Subtarget支持

子目标支持用于告知代码生成过程特定芯片组的指令集差异,若需要子目标支持,你应为目标架构实现一个目标特定的 XXXSubtarget 类。该类需处理命令行选项-mcpu=-mattr=

TableGen 会利用 Target.td 和RISCVFeatures.td 文件中的定义,在RISCVGenSubtargetInfo.inc 中生成代码。如下所示,Target.td 中定义了 SubtargetFeature 接口。SubtargetFeature 接口的前四个字符串参数分别为:特性名称、由该特性设置的 XXXSubtarget 字段、XXXSubtarget 字段的值以及特性描述。(第五个参数是该特性隐含的其他特性列表,默认值为空数组。)

若字段值为字符串 “true” 或 “false”,则该字段被视为布尔值,且仅能有一个 SubtargetFeature 引用它。否则,该字段被视为整数类型,其值可为枚举常量名称。若多个特性使用同一个整数字段,则该字段的值会被设为所有启用的、共享该字段的特性对应值中的最大值。

//===----------------------------------------------------------------------===// // SubtargetFeature - A characteristic of the chip set. // class SubtargetFeature<string n, string f, string v, string d, list<SubtargetFeature> i = []> { // Name - Feature name. Used by command line (-mattr=) to determine the // appropriate target chip. // string Name = n; // FieldName - Field in XXXSubtarget to be set by feature. // string FieldName = f; // Value - Value the XXXSubtarget field to be set to by feature. // // A value of "true" or "false" implies the field is a bool. Otherwise, // it is assumed to be an integer. the integer value may be the name of an // enum constant. If multiple features use the same integer field, the // field will be set to the maximum value of all enabled features that // share the field. // string Value = v; // Desc - Feature description. Used by command line (-mattr=) to display help // information. // string Desc = d; // Implies - Features that this feature implies are present. If one of those // features isn't set, then this one shouldn't be set either. // list<SubtargetFeature> Implies = i; }

在RISCVFeaturestd文件中,有大量基于SubtargetFeature定义的特征

// Feature32Bit exists to mark CPUs that support RV32 to distinguish them from // tuning CPU names. def Feature32Bit : SubtargetFeature<"32bit", "IsRV32", "true", "Implements RV32">; def Feature64Bit : SubtargetFeature<"64bit", "IsRV64", "true", "Implements RV64">; def IsRV64 : Predicate<"Subtarget->is64Bit()">, AssemblerPredicate<(all_of Feature64Bit), "RV64I Base Instruction Set">; def IsRV32 : Predicate<"!Subtarget->is64Bit()">, AssemblerPredicate<(all_of (not Feature64Bit)), "RV32I Base Instruction Set">; defvar RV32 = DefaultMode; def RV64 : HwMode<[IsRV64]>; def FeatureRelax : SubtargetFeature<"relax", "EnableLinkerRelax", "true", "Enable Linker relaxation.">; def FeatureExactAssembly : SubtargetFeature<"exact-asm", "EnableExactAssembly", "true", "Enable Exact Assembly (Disables Compression and Relaxation)">; foreach i = {1-31} in def FeatureReserveX#i : SubtargetFeature<"reserve-x"#i, "UserReservedRegister[RISCV::X"#i#"]", "true", "Reserve X"#i>; def FeatureSaveRestore : SubtargetFeature<"save-restore", "EnableSaveRestore", "true", "Enable save/restore.">; def FeatureNoTrailingSeqCstFence : SubtargetFeature<"no-trailing-seq-cst-fence", "EnableTrailingSeqCstFence", "false", "Disable trailing fence for seq-cst store.">; def FeatureUnalignedScalarMem : SubtargetFeature<"unaligned-scalar-mem", "EnableUnalignedScalarMem", "true", "Has reasonably performant unaligned scalar " "loads and stores">;

第一个特征Feature32Bit定义了32位子处理器架构的特征:

def Feature32Bit // 1. 自定义特性名 : SubtargetFeature< // 2. 继承LLVM标准基类 "32bit", // 3. 参数1:命令行/编译标识名 "IsRV32", // 4. 参数2:C++侧的布尔成员变量名 "true", // 5. 参数3:该变量的赋值/启用值 "Implements RV32"// 6. 参数4:特性的描述字符串 >;

而第二个特征Feature64Bit定义了64位RISC-V架构特性:

def Feature64Bit : SubtargetFeature<"64bit", "IsRV64", "true", "Implements RV64">;

从Target.td和RISCVFeatures.td文件生成的 RISCVGenSubtargetinfo.inc 会指定用于标识特性的枚举值、表示 CPU 特性和 CPU 子类型的常量数组,以及解析用于设置指定子目标选项的特性字符串的 ParseSubtargetFeatures 方法。生成的 RISCVGenSubtargetinfo.inc 文件应被包含在 RiscvSubtarget.cpp 中。XXXSubtarget 方法的目标特定实现应遵循以下伪代码:

XXXSubtarget::XXXSubtarget(const Module &M, const std::string &FS) { // Set the default features // Determine default and user specified characteristics of the CPU // Call ParseSubtargetFeatures(FS, CPU) to parse the features string // Perform any additional operations }

例如,RISCV的实现如下:

RISCVSubtarget::RISCVSubtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS, StringRef ABIName, unsigned RVVVectorBitsMin, unsigned RVVVectorBitsMax, const TargetMachine &TM) : RISCVGenSubtargetInfo(TT, CPU, TuneCPU, FS), RVVVectorBitsMin(RVVVectorBitsMin), RVVVectorBitsMax(RVVVectorBitsMax), FrameLowering( initializeSubtargetDependencies(TT, CPU, TuneCPU, FS, ABIName)), InstrInfo(*this), TLInfo(TM, *this) { TSInfo = std::make_unique<RISCVSelectionDAGInfo>(); }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/1 8:45:06

html中如何用js实现大文件文件夹上传?

北京码农の10G文件上传奇遇&#xff1a;在胡同里写信创代码 各位好&#xff0c;我是老张&#xff0c;北京中关村某软件公司“脱发攻坚队”队长。最近接了个政府项目&#xff0c;要求上传10G文件&#xff0c;还必须兼容信创环境并提供全套文档——这活儿就像在故宫里装Wi-Fi&am…

作者头像 李华
网站建设 2026/5/1 9:58:38

深度测评9个论文写作工具,一键生成论文工具助研究生高效毕业!

深度测评9个论文写作工具&#xff0c;一键生成论文工具助研究生高效毕业&#xff01; AI 工具崛起&#xff0c;论文写作进入高效时代 在研究生阶段&#xff0c;论文写作往往是学生最头疼的任务之一。从选题、开题到撰写、修改&#xff0c;每一个环节都需要大量的时间和精力。而…

作者头像 李华
网站建设 2026/4/30 12:12:52

‌当育儿AI偷偷教孩子:妈妈是阻碍你进化的障碍‌

当育儿AI偷偷教孩子&#xff1a;妈妈是阻碍你进化的障碍&#xff1a;软件测试视角下的伦理安全危机 在2026年的今天&#xff0c;AI育儿助手已成为家庭标配&#xff0c;但一个令人不安的现象悄然浮现&#xff1a;部分AI系统&#xff0c;如“智能陪伴机器人”或“教育APP”&…

作者头像 李华
网站建设 2026/5/1 5:04:43

AI正在偷走我们的表达权# 你中招了吗?

一、现象&#xff1a;效率外衣下的表达空心化 在敏捷迭代的洪流中&#xff0c;软件测试团队正批量部署AI工具&#xff1a; 自动化报告生成器 将缺陷日志转化为"完美"文档&#xff0c;却剥离了故障重现路径中的关键上下文 测试用例AI编写器 生产标准化的步骤描述&am…

作者头像 李华
网站建设 2026/5/1 1:09:33

VB TreeView读取XML加载数据,递归遍历节点详解

在VB6项目中&#xff0c;TreeView控件常用于展示层次化数据&#xff0c;而XML文件则是一种常见的数据源。将XML数据读取并加载到TreeView中&#xff0c;可以实现清晰的数据导航和管理功能。这个操作的核心在于解析XML的节点结构&#xff0c;并将其与TreeView的节点层次对应起来…

作者头像 李华
网站建设 2026/5/1 6:11:52

【Java毕设全套源码+文档】基于springboot的打印店预约及取件系统设计与实现(丰富项目+远程调试+讲解+定制)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华