news 2026/3/25 3:46:37

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/3/23 9:11:34

LLVM后端入门9:JIT支持

目标机器的实现可以选择性地包含一个即时(JIT)代码生成器,该生成器会将机器码和辅助结构作为二进制输出进行生成,这些输出可直接写入内存。要实现这一点,可通过执行以下步骤来实现 JIT 代码生成: 编写 XXXCodeEmitter.cpp 文件,其中包含一个机器函数 pass,用于将目标机…

作者头像 李华
网站建设 2026/3/21 9:24:27

系统软件缺少comct332.ocx无法启动 免费下载方法分享

在使用电脑系统时经常会出现丢失找不到某些文件的情况&#xff0c;由于很多常用软件都是采用 Microsoft Visual Studio 编写的&#xff0c;所以这类软件的运行需要依赖微软Visual C运行库&#xff0c;比如像 QQ、迅雷、Adobe 软件等等&#xff0c;如果没有安装VC运行库或者安装…

作者头像 李华
网站建设 2026/3/24 15:36:59

你不是在和AI竞争,你是在和“不用AI的自己”

导言&#xff1a;被误读的竞争关系 凌晨三点的告警邮件、重复的冒烟测试、永无止境的回归用例...当ChatGPT写出第一条测试脚本时&#xff0c;测试圈掀起海啸式恐慌。但真正需要警惕的并非AI&#xff0c;而是我们面对技术变革时固化的思维模式——软件测试的竞争本质&#xff0…

作者头像 李华
网站建设 2026/3/24 14:35:53

‌AI驱动的竞品App测试用例自动生成

AI已能系统化生成竞品测试用例&#xff0c;但需构建“行为建模→差异识别→优先级排序”闭环流程‌当前&#xff0c;软件测试领域正经历从“人工枚举功能点”向“AI驱动行为分析”的范式迁移。基于对50竞品App的自动化分析&#xff0c;可实现测试用例的高效生成&#xff0c;其核…

作者头像 李华
网站建设 2026/3/15 10:59:42

【Java毕设源码分享】基于springboot+vue的露营地管理系统的设计与实现(程序+文档+代码讲解+一条龙定制)

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

作者头像 李华
网站建设 2026/3/14 17:37:27

如何在HTML5中使用js实现文件夹上传?

第一章&#xff1a;毕业设计の终极挑战 "同学&#xff0c;你这毕业设计要做文件管理系统&#xff1f;还要支持10G大文件上传&#xff1f;"导师推了推眼镜&#xff0c;我仿佛看到他头顶飘着"这届学生真难带"的弹幕。 "是的老师&#xff01;还要兼容I…

作者头像 李华