|
| 1 | +//===- RustDebugInfo.cpp - Implementaion of Rust Debug Info Parser ---===// |
| 2 | +// |
| 3 | +// Enzyme Project |
| 4 | +// |
| 5 | +// Part of the Enzyme Project, under the Apache License v2.0 with LLVM |
| 6 | +// Exceptions. See https://llvm.org/LICENSE.txt for license information. |
| 7 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | +// |
| 9 | +// If using this code in an academic setting, please cite the following: |
| 10 | +// @incollection{enzymeNeurips, |
| 11 | +// title = {Instead of Rewriting Foreign Code for Machine Learning, |
| 12 | +// Automatically Synthesize Fast Gradients}, |
| 13 | +// author = {Moses, William S. and Churavy, Valentin}, |
| 14 | +// booktitle = {Advances in Neural Information Processing Systems 33}, |
| 15 | +// year = {2020}, |
| 16 | +// note = {To appear in}, |
| 17 | +// } |
| 18 | +// |
| 19 | +//===-------------------------------------------------------------------===// |
| 20 | +// |
| 21 | +// This file implement the Rust debug info parsing function. It will get the |
| 22 | +// description of types from debug info of an instruction and pass it to |
| 23 | +// concrete functions according to the kind of a description and construct |
| 24 | +// the type tree recursively. |
| 25 | +// |
| 26 | +//===-------------------------------------------------------------------===// |
| 27 | +#include "llvm/IR/DataLayout.h" |
| 28 | +#include "llvm/IR/DebugInfo.h" |
| 29 | +#include "llvm/Support/CommandLine.h" |
| 30 | + |
| 31 | +#include "RustDebugInfo.h" |
| 32 | + |
| 33 | +TypeTree parseDIType(DIType &Type, Instruction &I, DataLayout &DL); |
| 34 | + |
| 35 | +TypeTree parseDIType(DIBasicType &Type, Instruction &I, DataLayout &DL) { |
| 36 | + std::string TypeName = Type.getName().str(); |
| 37 | + TypeTree Result; |
| 38 | + if (TypeName == "f64") { |
| 39 | + Result = TypeTree(Type::getDoubleTy(I.getContext())).Only(0); |
| 40 | + } else if (TypeName == "f32") { |
| 41 | + Result = TypeTree(Type::getFloatTy(I.getContext())).Only(0); |
| 42 | + } else if (TypeName == "i8" || TypeName == "i16" || TypeName == "i32" || |
| 43 | + TypeName == "i64" || TypeName == "isize" || TypeName == "u8" || |
| 44 | + TypeName == "u16" || TypeName == "u32" || TypeName == "u64" || |
| 45 | + TypeName == "usize" || TypeName == "i128" || TypeName == "u128") { |
| 46 | + Result = TypeTree(ConcreteType(BaseType::Integer)).Only(0); |
| 47 | + } else { |
| 48 | + Result = TypeTree(ConcreteType(BaseType::Unknown)).Only(0); |
| 49 | + } |
| 50 | + return Result; |
| 51 | +} |
| 52 | + |
| 53 | +TypeTree parseDIType(DICompositeType &Type, Instruction &I, DataLayout &DL) { |
| 54 | + TypeTree Result; |
| 55 | + if (Type.getTag() == dwarf::DW_TAG_array_type) { |
| 56 | +#if LLVM_VERSION_MAJOR >= 9 |
| 57 | + DIType *SubType = Type.getBaseType(); |
| 58 | +#else |
| 59 | + DIType *SubType = Type.getBaseType().resolve(); |
| 60 | +#endif |
| 61 | + TypeTree SubTT = parseDIType(*SubType, I, DL); |
| 62 | + size_t Align = Type.getAlignInBytes(); |
| 63 | + size_t SubSize = SubType->getSizeInBits() / 8; |
| 64 | + size_t Size = Type.getSizeInBits() / 8; |
| 65 | + DINodeArray Subranges = Type.getElements(); |
| 66 | + size_t pos = 0; |
| 67 | + for (auto r : Subranges) { |
| 68 | + DISubrange *Subrange = dyn_cast<DISubrange>(r); |
| 69 | + if (auto Count = Subrange->getCount().get<ConstantInt *>()) { |
| 70 | + int64_t count = Count->getSExtValue(); |
| 71 | + if (count == -1) { |
| 72 | + break; |
| 73 | + } |
| 74 | + for (int64_t i = 0; i < count; i++) { |
| 75 | + Result |= SubTT.ShiftIndices(DL, 0, Size, pos); |
| 76 | + size_t tmp = pos + SubSize; |
| 77 | + if (tmp % Align != 0) { |
| 78 | + pos = (tmp / Align + 1) * Align; |
| 79 | + } else { |
| 80 | + pos = tmp; |
| 81 | + } |
| 82 | + } |
| 83 | + } else { |
| 84 | + assert(0 && "There shouldn't be non-constant-size arrays in Rust"); |
| 85 | + } |
| 86 | + } |
| 87 | + return Result; |
| 88 | + } else if (Type.getTag() == dwarf::DW_TAG_structure_type || |
| 89 | + Type.getTag() == dwarf::DW_TAG_union_type) { |
| 90 | + DINodeArray Elements = Type.getElements(); |
| 91 | + size_t Size = Type.getSizeInBits() / 8; |
| 92 | + bool firstSubTT = true; |
| 93 | + for (auto e : Elements) { |
| 94 | + DIType *SubType = dyn_cast<DIDerivedType>(e); |
| 95 | + assert(SubType->getTag() == dwarf::DW_TAG_member); |
| 96 | + TypeTree SubTT = parseDIType(*SubType, I, DL); |
| 97 | + size_t Offset = SubType->getOffsetInBits() / 8; |
| 98 | + SubTT = SubTT.ShiftIndices(DL, 0, Size, Offset); |
| 99 | + if (Type.getTag() == dwarf::DW_TAG_structure_type) { |
| 100 | + Result |= SubTT; |
| 101 | + } else { |
| 102 | + if (firstSubTT) { |
| 103 | + Result = SubTT; |
| 104 | + } else { |
| 105 | + Result &= SubTT; |
| 106 | + } |
| 107 | + } |
| 108 | + if (firstSubTT) { |
| 109 | + firstSubTT = !firstSubTT; |
| 110 | + } |
| 111 | + } |
| 112 | + return Result; |
| 113 | + } else { |
| 114 | + assert(0 && "Composite types other than arrays, structs and unions are not " |
| 115 | + "supported by Rust debug info parser"); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +TypeTree parseDIType(DIDerivedType &Type, Instruction &I, DataLayout &DL) { |
| 120 | + if (Type.getTag() == dwarf::DW_TAG_pointer_type) { |
| 121 | + TypeTree Result(BaseType::Pointer); |
| 122 | +#if LLVM_VERSION_MAJOR >= 9 |
| 123 | + DIType *SubType = Type.getBaseType(); |
| 124 | +#else |
| 125 | + DIType *SubType = Type.getBaseType().resolve(); |
| 126 | +#endif |
| 127 | + TypeTree SubTT = parseDIType(*SubType, I, DL); |
| 128 | + if (isa<DIBasicType>(SubType)) { |
| 129 | + Result |= SubTT.ShiftIndices(DL, 0, 1, -1); |
| 130 | + } else { |
| 131 | + Result |= SubTT; |
| 132 | + } |
| 133 | + return Result.Only(0); |
| 134 | + } else if (Type.getTag() == dwarf::DW_TAG_member) { |
| 135 | +#if LLVM_VERSION_MAJOR >= 9 |
| 136 | + DIType *SubType = Type.getBaseType(); |
| 137 | +#else |
| 138 | + DIType *SubType = Type.getBaseType().resolve(); |
| 139 | +#endif |
| 140 | + TypeTree Result = parseDIType(*SubType, I, DL); |
| 141 | + return Result; |
| 142 | + } else { |
| 143 | + assert(0 && "Derived types other than pointers and members are not " |
| 144 | + "supported by Rust debug info parser"); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +TypeTree parseDIType(DIType &Type, Instruction &I, DataLayout &DL) { |
| 149 | + if (Type.getSizeInBits() == 0) { |
| 150 | + return TypeTree(); |
| 151 | + } |
| 152 | + |
| 153 | + if (auto BT = dyn_cast<DIBasicType>(&Type)) { |
| 154 | + return parseDIType(*BT, I, DL); |
| 155 | + } else if (auto CT = dyn_cast<DICompositeType>(&Type)) { |
| 156 | + return parseDIType(*CT, I, DL); |
| 157 | + } else if (auto DT = dyn_cast<DIDerivedType>(&Type)) { |
| 158 | + return parseDIType(*DT, I, DL); |
| 159 | + } else { |
| 160 | + assert(0 && "Types other than floating-points, integers, arrays, pointers, " |
| 161 | + "slices, and structs are not supported by debug info parser"); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +bool isU8PointerType(DIType &type) { |
| 166 | + if (type.getTag() == dwarf::DW_TAG_pointer_type) { |
| 167 | + auto PTy = dyn_cast<DIDerivedType>(&type); |
| 168 | +#if LLVM_VERSION_MAJOR >= 9 |
| 169 | + DIType *SubType = PTy->getBaseType(); |
| 170 | +#else |
| 171 | + DIType *SubType = PTy->getBaseType().resolve(); |
| 172 | +#endif |
| 173 | + if (auto BTy = dyn_cast<DIBasicType>(SubType)) { |
| 174 | + std::string name = BTy->getName().str(); |
| 175 | + if (name == "u8") { |
| 176 | + return true; |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + return false; |
| 181 | +} |
| 182 | + |
| 183 | +TypeTree parseDIType(DbgDeclareInst &I, DataLayout &DL) { |
| 184 | +#if LLVM_VERSION_MAJOR >= 9 |
| 185 | + DIType *type = I.getVariable()->getType(); |
| 186 | +#else |
| 187 | + DIType *type = I.getVariable()->getType().resolve(); |
| 188 | +#endif |
| 189 | + |
| 190 | + // If the type is *u8, do nothing, since the underlying type of data pointed |
| 191 | + // by a *u8 can be anything |
| 192 | + if (isU8PointerType(*type)) { |
| 193 | + return TypeTree(); |
| 194 | + } |
| 195 | + TypeTree Result = parseDIType(*type, I, DL); |
| 196 | + return Result; |
| 197 | +} |
0 commit comments