| 1 | //===- MIRParser.cpp - MIR serialization format parser implementation -----===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the class that parses the optional LLVM IR and machine |
| 10 | // functions that are stored in MIR files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/MIRParser/MIRParser.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/AsmParser/Parser.h" |
| 18 | #include "llvm/AsmParser/SlotMapping.h" |
| 19 | #include "llvm/CodeGen/MIRParser/MIParser.h" |
| 20 | #include "llvm/CodeGen/MIRYamlMapping.h" |
| 21 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
| 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 27 | #include "llvm/CodeGen/TargetFrameLowering.h" |
| 28 | #include "llvm/IR/BasicBlock.h" |
| 29 | #include "llvm/IR/DebugInfoMetadata.h" |
| 30 | #include "llvm/IR/DiagnosticInfo.h" |
| 31 | #include "llvm/IR/Instructions.h" |
| 32 | #include "llvm/IR/LLVMContext.h" |
| 33 | #include "llvm/IR/Module.h" |
| 34 | #include "llvm/IR/ValueSymbolTable.h" |
| 35 | #include "llvm/Support/LineIterator.h" |
| 36 | #include "llvm/Support/MemoryBuffer.h" |
| 37 | #include "llvm/Support/SMLoc.h" |
| 38 | #include "llvm/Support/SourceMgr.h" |
| 39 | #include "llvm/Support/YAMLTraits.h" |
| 40 | #include "llvm/Target/TargetMachine.h" |
| 41 | #include <memory> |
| 42 | |
| 43 | using namespace llvm; |
| 44 | |
| 45 | namespace llvm { |
| 46 | class MDNode; |
| 47 | class RegisterBank; |
| 48 | |
| 49 | /// This class implements the parsing of LLVM IR that's embedded inside a MIR |
| 50 | /// file. |
| 51 | class MIRParserImpl { |
| 52 | SourceMgr SM; |
| 53 | LLVMContext &Context; |
| 54 | yaml::Input In; |
| 55 | StringRef Filename; |
| 56 | SlotMapping IRSlots; |
| 57 | std::unique_ptr<PerTargetMIParsingState> Target; |
| 58 | |
| 59 | /// True when the MIR file doesn't have LLVM IR. Dummy IR functions are |
| 60 | /// created and inserted into the given module when this is true. |
| 61 | bool NoLLVMIR = false; |
| 62 | /// True when a well formed MIR file does not contain any MIR/machine function |
| 63 | /// parts. |
| 64 | bool NoMIRDocuments = false; |
| 65 | |
| 66 | std::function<void(Function &)> ProcessIRFunction; |
| 67 | |
| 68 | public: |
| 69 | MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename, |
| 70 | LLVMContext &Context, |
| 71 | std::function<void(Function &)> ProcessIRFunction); |
| 72 | |
| 73 | void reportDiagnostic(const SMDiagnostic &Diag); |
| 74 | |
| 75 | /// Report an error with the given message at unknown location. |
| 76 | /// |
| 77 | /// Always returns true. |
| 78 | bool error(const Twine &Message); |
| 79 | |
| 80 | /// Report an error with the given message at the given location. |
| 81 | /// |
| 82 | /// Always returns true. |
| 83 | bool error(SMLoc Loc, const Twine &Message); |
| 84 | |
| 85 | /// Report a given error with the location translated from the location in an |
| 86 | /// embedded string literal to a location in the MIR file. |
| 87 | /// |
| 88 | /// Always returns true. |
| 89 | bool error(const SMDiagnostic &Error, SMRange SourceRange); |
| 90 | |
| 91 | /// Try to parse the optional LLVM module and the machine functions in the MIR |
| 92 | /// file. |
| 93 | /// |
| 94 | /// Return null if an error occurred. |
| 95 | std::unique_ptr<Module> |
| 96 | parseIRModule(DataLayoutCallbackTy DataLayoutCallback); |
| 97 | |
| 98 | /// Create an empty function with the given name. |
| 99 | Function *createDummyFunction(StringRef Name, Module &M); |
| 100 | |
| 101 | bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI, |
| 102 | ModuleAnalysisManager *FAM = nullptr); |
| 103 | |
| 104 | /// Parse the machine function in the current YAML document. |
| 105 | /// |
| 106 | /// |
| 107 | /// Return true if an error occurred. |
| 108 | bool parseMachineFunction(Module &M, MachineModuleInfo &MMI, |
| 109 | ModuleAnalysisManager *FAM, |
| 110 | Module::iterator &FirstUnvisitedFunction); |
| 111 | |
| 112 | /// Initialize the machine function to the state that's described in the MIR |
| 113 | /// file. |
| 114 | /// |
| 115 | /// Return true if error occurred. |
| 116 | bool initializeMachineFunction(const yaml::MachineFunction &YamlMF, |
| 117 | MachineFunction &MF); |
| 118 | |
| 119 | bool initializeCallSiteInfo(PerFunctionMIParsingState &PFS, |
| 120 | const yaml::MachineFunction &YamlMF); |
| 121 | |
| 122 | bool initializePrefetchTargets(PerFunctionMIParsingState &PFS, |
| 123 | const yaml::MachineFunction &YamlMF); |
| 124 | |
| 125 | bool parseRegisterInfo(PerFunctionMIParsingState &PFS, |
| 126 | const yaml::MachineFunction &YamlMF); |
| 127 | |
| 128 | bool setupRegisterInfo(const PerFunctionMIParsingState &PFS, |
| 129 | const yaml::MachineFunction &YamlMF); |
| 130 | |
| 131 | bool initializeFrameInfo(PerFunctionMIParsingState &PFS, |
| 132 | const yaml::MachineFunction &YamlMF); |
| 133 | |
| 134 | bool initializeSaveRestorePoints( |
| 135 | PerFunctionMIParsingState &PFS, |
| 136 | const std::vector<yaml::SaveRestorePointEntry> &YamlSRPoints, |
| 137 | llvm::SaveRestorePoints &SaveRestorePoints); |
| 138 | |
| 139 | bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS, |
| 140 | std::vector<CalleeSavedInfo> &CSIInfo, |
| 141 | const yaml::StringValue &RegisterSource, |
| 142 | bool IsRestored, int FrameIdx); |
| 143 | |
| 144 | struct VarExprLoc { |
| 145 | DILocalVariable *DIVar = nullptr; |
| 146 | DIExpression *DIExpr = nullptr; |
| 147 | DILocation *DILoc = nullptr; |
| 148 | }; |
| 149 | |
| 150 | std::optional<VarExprLoc> parseVarExprLoc(PerFunctionMIParsingState &PFS, |
| 151 | const yaml::StringValue &VarStr, |
| 152 | const yaml::StringValue &ExprStr, |
| 153 | const yaml::StringValue &LocStr); |
| 154 | template <typename T> |
| 155 | bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS, |
| 156 | const T &Object, |
| 157 | int FrameIdx); |
| 158 | |
| 159 | bool initializeConstantPool(PerFunctionMIParsingState &PFS, |
| 160 | MachineConstantPool &ConstantPool, |
| 161 | const yaml::MachineFunction &YamlMF); |
| 162 | |
| 163 | bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS, |
| 164 | const yaml::MachineJumpTable &YamlJTI); |
| 165 | |
| 166 | bool parseMachineMetadataNodes(PerFunctionMIParsingState &PFS, |
| 167 | MachineFunction &MF, |
| 168 | const yaml::MachineFunction &YMF); |
| 169 | |
| 170 | bool parseCalledGlobals(PerFunctionMIParsingState &PFS, MachineFunction &MF, |
| 171 | const yaml::MachineFunction &YMF); |
| 172 | |
| 173 | private: |
| 174 | bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, |
| 175 | const yaml::StringValue &Source); |
| 176 | |
| 177 | bool parseMBBReference(PerFunctionMIParsingState &PFS, |
| 178 | MachineBasicBlock *&MBB, |
| 179 | const yaml::StringValue &Source); |
| 180 | |
| 181 | bool parseMachineMetadata(PerFunctionMIParsingState &PFS, |
| 182 | const yaml::StringValue &Source); |
| 183 | |
| 184 | /// Return a MIR diagnostic converted from an MI string diagnostic. |
| 185 | SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error, |
| 186 | SMRange SourceRange); |
| 187 | |
| 188 | /// Return a MIR diagnostic converted from a diagnostic located in a YAML |
| 189 | /// block scalar string. |
| 190 | SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error, |
| 191 | SMRange SourceRange); |
| 192 | |
| 193 | bool computeFunctionProperties(MachineFunction &MF, |
| 194 | const yaml::MachineFunction &YamlMF); |
| 195 | |
| 196 | void setupDebugValueTracking(MachineFunction &MF, |
| 197 | PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF); |
| 198 | |
| 199 | bool parseMachineInst(MachineFunction &MF, yaml::MachineInstrLoc MILoc, |
| 200 | MachineInstr const *&MI); |
| 201 | }; |
| 202 | |
| 203 | } // end namespace llvm |
| 204 | |
| 205 | static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { |
| 206 | reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag); |
| 207 | } |
| 208 | |
| 209 | MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, |
| 210 | StringRef Filename, LLVMContext &Context, |
| 211 | std::function<void(Function &)> Callback) |
| 212 | : Context(Context), |
| 213 | In(SM.getMemoryBuffer(i: SM.AddNewSourceBuffer(F: std::move(Contents), IncludeLoc: SMLoc())) |
| 214 | ->getBuffer(), |
| 215 | nullptr, handleYAMLDiag, this), |
| 216 | Filename(Filename), ProcessIRFunction(Callback) { |
| 217 | In.setContext(&In); |
| 218 | } |
| 219 | |
| 220 | bool MIRParserImpl::error(const Twine &Message) { |
| 221 | Context.diagnose(DI: DiagnosticInfoMIRParser( |
| 222 | DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str()))); |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) { |
| 227 | Context.diagnose(DI: DiagnosticInfoMIRParser( |
| 228 | DS_Error, SM.GetMessage(Loc, Kind: SourceMgr::DK_Error, Msg: Message))); |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) { |
| 233 | assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error" ); |
| 234 | reportDiagnostic(Diag: diagFromMIStringDiag(Error, SourceRange)); |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) { |
| 239 | DiagnosticSeverity Kind; |
| 240 | switch (Diag.getKind()) { |
| 241 | case SourceMgr::DK_Error: |
| 242 | Kind = DS_Error; |
| 243 | break; |
| 244 | case SourceMgr::DK_Warning: |
| 245 | Kind = DS_Warning; |
| 246 | break; |
| 247 | case SourceMgr::DK_Note: |
| 248 | Kind = DS_Note; |
| 249 | break; |
| 250 | case SourceMgr::DK_Remark: |
| 251 | llvm_unreachable("remark unexpected" ); |
| 252 | break; |
| 253 | } |
| 254 | Context.diagnose(DI: DiagnosticInfoMIRParser(Kind, Diag)); |
| 255 | } |
| 256 | |
| 257 | std::unique_ptr<Module> |
| 258 | MIRParserImpl::parseIRModule(DataLayoutCallbackTy DataLayoutCallback) { |
| 259 | if (!In.setCurrentDocument()) { |
| 260 | if (In.error()) |
| 261 | return nullptr; |
| 262 | // Create an empty module when the MIR file is empty. |
| 263 | NoMIRDocuments = true; |
| 264 | auto M = std::make_unique<Module>(args&: Filename, args&: Context); |
| 265 | if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple().str(), |
| 266 | M->getDataLayoutStr())) |
| 267 | M->setDataLayout(*LayoutOverride); |
| 268 | return M; |
| 269 | } |
| 270 | |
| 271 | std::unique_ptr<Module> M; |
| 272 | // Parse the block scalar manually so that we can return unique pointer |
| 273 | // without having to go trough YAML traits. |
| 274 | if (const auto *BSN = |
| 275 | dyn_cast_or_null<yaml::BlockScalarNode>(Val: In.getCurrentNode())) { |
| 276 | SMDiagnostic Error; |
| 277 | M = parseAssembly(F: MemoryBufferRef(BSN->getValue(), Filename), Err&: Error, |
| 278 | Context, Slots: &IRSlots, DataLayoutCallback); |
| 279 | if (!M) { |
| 280 | reportDiagnostic(Diag: diagFromBlockStringDiag(Error, SourceRange: BSN->getSourceRange())); |
| 281 | return nullptr; |
| 282 | } |
| 283 | In.nextDocument(); |
| 284 | if (!In.setCurrentDocument()) |
| 285 | NoMIRDocuments = true; |
| 286 | } else { |
| 287 | // Create an new, empty module. |
| 288 | M = std::make_unique<Module>(args&: Filename, args&: Context); |
| 289 | if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple().str(), |
| 290 | M->getDataLayoutStr())) |
| 291 | M->setDataLayout(*LayoutOverride); |
| 292 | NoLLVMIR = true; |
| 293 | } |
| 294 | return M; |
| 295 | } |
| 296 | |
| 297 | bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI, |
| 298 | ModuleAnalysisManager *MAM) { |
| 299 | if (NoMIRDocuments) |
| 300 | return false; |
| 301 | |
| 302 | // Parse the machine functions. |
| 303 | auto FirstUnvisitedFunction = M.begin(); |
| 304 | do { |
| 305 | if (parseMachineFunction(M, MMI, FAM: MAM, FirstUnvisitedFunction)) |
| 306 | return true; |
| 307 | In.nextDocument(); |
| 308 | } while (In.setCurrentDocument()); |
| 309 | |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | Function *MIRParserImpl::createDummyFunction(StringRef Name, Module &M) { |
| 314 | auto &Context = M.getContext(); |
| 315 | Function *F = |
| 316 | Function::Create(Ty: FunctionType::get(Result: Type::getVoidTy(C&: Context), isVarArg: false), |
| 317 | Linkage: Function::ExternalLinkage, N: Name, M); |
| 318 | BasicBlock *BB = BasicBlock::Create(Context, Name: "entry" , Parent: F); |
| 319 | new UnreachableInst(Context, BB); |
| 320 | |
| 321 | if (ProcessIRFunction) |
| 322 | ProcessIRFunction(*F); |
| 323 | |
| 324 | return F; |
| 325 | } |
| 326 | |
| 327 | static Function * |
| 328 | getNextUnusedUnnamedFunction(const Module &M, |
| 329 | Module::iterator &FirstUnvisitedFunction) { |
| 330 | for (; FirstUnvisitedFunction != M.end(); ++FirstUnvisitedFunction) |
| 331 | if (!FirstUnvisitedFunction->hasName()) |
| 332 | return &*FirstUnvisitedFunction++; |
| 333 | |
| 334 | return nullptr; |
| 335 | } |
| 336 | |
| 337 | bool MIRParserImpl::parseMachineFunction( |
| 338 | Module &M, MachineModuleInfo &MMI, ModuleAnalysisManager *MAM, |
| 339 | Module::iterator &FirstUnvisitedFunction) { |
| 340 | // Parse the yaml. |
| 341 | yaml::MachineFunction YamlMF; |
| 342 | yaml::EmptyContext Ctx; |
| 343 | |
| 344 | const TargetMachine &TM = MMI.getTarget(); |
| 345 | YamlMF.MachineFuncInfo = std::unique_ptr<yaml::MachineFunctionInfo>( |
| 346 | TM.createDefaultFuncInfoYAML()); |
| 347 | |
| 348 | yaml::yamlize(io&: In, Val&: YamlMF, false, Ctx); |
| 349 | if (In.error()) |
| 350 | return true; |
| 351 | |
| 352 | // Search for the corresponding IR function. |
| 353 | StringRef FunctionName = YamlMF.Name; |
| 354 | Function *F = M.getFunction(Name: FunctionName); |
| 355 | if (!F) { |
| 356 | if (NoLLVMIR) { |
| 357 | F = createDummyFunction(Name: FunctionName, M); |
| 358 | } else if (!FunctionName.empty() || |
| 359 | !(F = getNextUnusedUnnamedFunction(M, FirstUnvisitedFunction))) { |
| 360 | return error(Message: Twine("function '" ) + FunctionName + |
| 361 | "' isn't defined in the provided LLVM IR" ); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if (!MAM) { |
| 366 | if (MMI.getMachineFunction(F: *F) != nullptr) |
| 367 | return error(Message: Twine("redefinition of machine function '" ) + FunctionName + |
| 368 | "'" ); |
| 369 | |
| 370 | // Create the MachineFunction. |
| 371 | MachineFunction &MF = MMI.getOrCreateMachineFunction(F&: *F); |
| 372 | if (initializeMachineFunction(YamlMF, MF)) |
| 373 | return true; |
| 374 | } else { |
| 375 | auto &FAM = |
| 376 | MAM->getResult<FunctionAnalysisManagerModuleProxy>(IR&: M).getManager(); |
| 377 | if (FAM.getCachedResult<MachineFunctionAnalysis>(IR&: *F)) |
| 378 | return error(Message: Twine("redefinition of machine function '" ) + FunctionName + |
| 379 | "'" ); |
| 380 | |
| 381 | // Create the MachineFunction. |
| 382 | MachineFunction &MF = FAM.getResult<MachineFunctionAnalysis>(IR&: *F).getMF(); |
| 383 | if (initializeMachineFunction(YamlMF, MF)) |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | static bool isSSA(const MachineFunction &MF) { |
| 391 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 392 | for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) { |
| 393 | Register Reg = Register::index2VirtReg(Index: I); |
| 394 | if (!MRI.hasOneDef(RegNo: Reg) && !MRI.def_empty(RegNo: Reg)) |
| 395 | return false; |
| 396 | |
| 397 | // Subregister defs are invalid in SSA. |
| 398 | const MachineOperand *RegDef = MRI.getOneDef(Reg); |
| 399 | if (RegDef && RegDef->getSubReg() != 0) |
| 400 | return false; |
| 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | bool MIRParserImpl::computeFunctionProperties( |
| 406 | MachineFunction &MF, const yaml::MachineFunction &YamlMF) { |
| 407 | MachineFunctionProperties &Properties = MF.getProperties(); |
| 408 | |
| 409 | bool HasPHI = false; |
| 410 | bool HasInlineAsm = false; |
| 411 | bool HasFakeUses = false; |
| 412 | bool AllTiedOpsRewritten = true, HasTiedOps = false; |
| 413 | for (const MachineBasicBlock &MBB : MF) { |
| 414 | for (const MachineInstr &MI : MBB) { |
| 415 | if (MI.isPHI()) |
| 416 | HasPHI = true; |
| 417 | if (MI.isInlineAsm()) |
| 418 | HasInlineAsm = true; |
| 419 | if (MI.isFakeUse()) |
| 420 | HasFakeUses = true; |
| 421 | for (unsigned I = 0; I < MI.getNumOperands(); ++I) { |
| 422 | const MachineOperand &MO = MI.getOperand(i: I); |
| 423 | if (!MO.isReg() || !MO.getReg()) |
| 424 | continue; |
| 425 | unsigned DefIdx; |
| 426 | if (MO.isUse() && MI.isRegTiedToDefOperand(UseOpIdx: I, DefOpIdx: &DefIdx)) { |
| 427 | HasTiedOps = true; |
| 428 | if (MO.getReg() != MI.getOperand(i: DefIdx).getReg()) |
| 429 | AllTiedOpsRewritten = false; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Helper function to sanity-check and set properties that are computed, but |
| 436 | // may be explicitly set from the input MIR |
| 437 | auto ComputedPropertyHelper = |
| 438 | [&Properties](std::optional<bool> ExplicitProp, bool ComputedProp, |
| 439 | MachineFunctionProperties::Property P) -> bool { |
| 440 | // Prefer explicitly given values over the computed properties |
| 441 | if (ExplicitProp.value_or(u&: ComputedProp)) |
| 442 | Properties.set(P); |
| 443 | else |
| 444 | Properties.reset(P); |
| 445 | |
| 446 | // Check for conflict between the explicit values and the computed ones |
| 447 | return ExplicitProp && *ExplicitProp && !ComputedProp; |
| 448 | }; |
| 449 | |
| 450 | if (ComputedPropertyHelper(YamlMF.NoPHIs, !HasPHI, |
| 451 | MachineFunctionProperties::Property::NoPHIs)) { |
| 452 | return error(Message: MF.getName() + |
| 453 | " has explicit property NoPhi, but contains at least one PHI" ); |
| 454 | } |
| 455 | |
| 456 | MF.setHasInlineAsm(HasInlineAsm); |
| 457 | |
| 458 | if (HasTiedOps && AllTiedOpsRewritten) |
| 459 | Properties.setTiedOpsRewritten(); |
| 460 | |
| 461 | if (ComputedPropertyHelper(YamlMF.IsSSA, isSSA(MF), |
| 462 | MachineFunctionProperties::Property::IsSSA)) { |
| 463 | return error(Message: MF.getName() + |
| 464 | " has explicit property IsSSA, but is not valid SSA" ); |
| 465 | } |
| 466 | |
| 467 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 468 | if (ComputedPropertyHelper(YamlMF.NoVRegs, MRI.getNumVirtRegs() == 0, |
| 469 | MachineFunctionProperties::Property::NoVRegs)) { |
| 470 | return error( |
| 471 | Message: MF.getName() + |
| 472 | " has explicit property NoVRegs, but contains virtual registers" ); |
| 473 | } |
| 474 | |
| 475 | // For hasFakeUses we follow similar logic to the ComputedPropertyHelper, |
| 476 | // except for caring about the inverse case only, i.e. when the property is |
| 477 | // explicitly set to false and Fake Uses are present; having HasFakeUses=true |
| 478 | // on a function without fake uses is harmless. |
| 479 | if (YamlMF.HasFakeUses && !*YamlMF.HasFakeUses && HasFakeUses) |
| 480 | return error( |
| 481 | Message: MF.getName() + |
| 482 | " has explicit property hasFakeUses=false, but contains fake uses" ); |
| 483 | MF.setHasFakeUses(YamlMF.HasFakeUses.value_or(u&: HasFakeUses)); |
| 484 | |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | bool MIRParserImpl::parseMachineInst(MachineFunction &MF, |
| 489 | yaml::MachineInstrLoc MILoc, |
| 490 | MachineInstr const *&MI) { |
| 491 | if (MILoc.BlockNum >= MF.size()) { |
| 492 | return error(Message: Twine(MF.getName()) + |
| 493 | Twine(" instruction block out of range." ) + |
| 494 | " Unable to reference bb:" + Twine(MILoc.BlockNum)); |
| 495 | } |
| 496 | auto BB = std::next(x: MF.begin(), n: MILoc.BlockNum); |
| 497 | if (MILoc.Offset >= BB->size()) |
| 498 | return error( |
| 499 | Message: Twine(MF.getName()) + Twine(" instruction offset out of range." ) + |
| 500 | " Unable to reference instruction at bb: " + Twine(MILoc.BlockNum) + |
| 501 | " at offset:" + Twine(MILoc.Offset)); |
| 502 | MI = &*std::next(x: BB->instr_begin(), n: MILoc.Offset); |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | bool MIRParserImpl::initializeCallSiteInfo( |
| 507 | PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF) { |
| 508 | MachineFunction &MF = PFS.MF; |
| 509 | SMDiagnostic Error; |
| 510 | const TargetMachine &TM = MF.getTarget(); |
| 511 | for (auto &YamlCSInfo : YamlMF.CallSitesInfo) { |
| 512 | yaml::MachineInstrLoc MILoc = YamlCSInfo.CallLocation; |
| 513 | const MachineInstr *CallI; |
| 514 | if (parseMachineInst(MF, MILoc, MI&: CallI)) |
| 515 | return true; |
| 516 | if (!CallI->isCall(Type: MachineInstr::IgnoreBundle)) |
| 517 | return error(Message: Twine(MF.getName()) + |
| 518 | Twine(" call site info should reference call " |
| 519 | "instruction. Instruction at bb:" ) + |
| 520 | Twine(MILoc.BlockNum) + " at offset:" + Twine(MILoc.Offset) + |
| 521 | " is not a call instruction" ); |
| 522 | MachineFunction::CallSiteInfo CSInfo; |
| 523 | for (auto ArgRegPair : YamlCSInfo.ArgForwardingRegs) { |
| 524 | Register Reg; |
| 525 | if (parseNamedRegisterReference(PFS, Reg, Src: ArgRegPair.Reg.Value, Error)) |
| 526 | return error(Error, SourceRange: ArgRegPair.Reg.SourceRange); |
| 527 | CSInfo.ArgRegPairs.emplace_back(Args&: Reg, Args&: ArgRegPair.ArgNo); |
| 528 | } |
| 529 | if (!YamlCSInfo.CalleeTypeIds.empty()) { |
| 530 | for (auto CalleeTypeId : YamlCSInfo.CalleeTypeIds) { |
| 531 | IntegerType *Int64Ty = Type::getInt64Ty(C&: Context); |
| 532 | CSInfo.CalleeTypeIds.push_back(Elt: ConstantInt::get(Ty: Int64Ty, V: CalleeTypeId, |
| 533 | /*isSigned=*/IsSigned: false)); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | if (TM.Options.EmitCallSiteInfo || TM.Options.EmitCallGraphSection) |
| 538 | MF.addCallSiteInfo(CallI: &*CallI, CallInfo: std::move(CSInfo)); |
| 539 | } |
| 540 | |
| 541 | if (!YamlMF.CallSitesInfo.empty() && |
| 542 | !(TM.Options.EmitCallSiteInfo || TM.Options.EmitCallGraphSection)) |
| 543 | return error(Message: "call site info provided but not used" ); |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | void MIRParserImpl::setupDebugValueTracking( |
| 548 | MachineFunction &MF, PerFunctionMIParsingState &PFS, |
| 549 | const yaml::MachineFunction &YamlMF) { |
| 550 | // Compute the value of the "next instruction number" field. |
| 551 | unsigned MaxInstrNum = 0; |
| 552 | for (auto &MBB : MF) |
| 553 | for (auto &MI : MBB) |
| 554 | MaxInstrNum = std::max(a: MI.peekDebugInstrNum(), b: MaxInstrNum); |
| 555 | MF.setDebugInstrNumberingCount(MaxInstrNum); |
| 556 | |
| 557 | // Load any substitutions. |
| 558 | for (const auto &Sub : YamlMF.DebugValueSubstitutions) { |
| 559 | MF.makeDebugValueSubstitution({Sub.SrcInst, Sub.SrcOp}, |
| 560 | {Sub.DstInst, Sub.DstOp}, SubReg: Sub.Subreg); |
| 561 | } |
| 562 | |
| 563 | // Flag for whether we're supposed to be using DBG_INSTR_REF. |
| 564 | MF.setUseDebugInstrRef(YamlMF.UseDebugInstrRef); |
| 565 | } |
| 566 | |
| 567 | bool |
| 568 | MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF, |
| 569 | MachineFunction &MF) { |
| 570 | // TODO: Recreate the machine function. |
| 571 | if (Target) { |
| 572 | // Avoid clearing state if we're using the same subtarget again. |
| 573 | Target->setTarget(MF.getSubtarget()); |
| 574 | } else { |
| 575 | Target.reset(p: new PerTargetMIParsingState(MF.getSubtarget())); |
| 576 | } |
| 577 | |
| 578 | MF.setAlignment(YamlMF.Alignment.valueOrOne()); |
| 579 | MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice); |
| 580 | MF.setHasWinCFI(YamlMF.HasWinCFI); |
| 581 | |
| 582 | MF.setCallsEHReturn(YamlMF.CallsEHReturn); |
| 583 | MF.setCallsUnwindInit(YamlMF.CallsUnwindInit); |
| 584 | MF.setHasEHContTarget(YamlMF.HasEHContTarget); |
| 585 | MF.setHasEHScopes(YamlMF.HasEHScopes); |
| 586 | MF.setHasEHFunclets(YamlMF.HasEHFunclets); |
| 587 | MF.setIsOutlined(YamlMF.IsOutlined); |
| 588 | |
| 589 | MachineFunctionProperties &Props = MF.getProperties(); |
| 590 | if (YamlMF.Legalized) |
| 591 | Props.setLegalized(); |
| 592 | if (YamlMF.RegBankSelected) |
| 593 | Props.setRegBankSelected(); |
| 594 | if (YamlMF.Selected) |
| 595 | Props.setSelected(); |
| 596 | if (YamlMF.FailedISel) |
| 597 | Props.setFailedISel(); |
| 598 | if (YamlMF.FailsVerification) |
| 599 | Props.setFailsVerification(); |
| 600 | if (YamlMF.TracksDebugUserValues) |
| 601 | Props.setTracksDebugUserValues(); |
| 602 | |
| 603 | PerFunctionMIParsingState PFS(MF, SM, IRSlots, *Target); |
| 604 | if (parseRegisterInfo(PFS, YamlMF)) |
| 605 | return true; |
| 606 | if (initializePrefetchTargets(PFS, YamlMF)) |
| 607 | return true; |
| 608 | if (!YamlMF.Constants.empty()) { |
| 609 | auto *ConstantPool = MF.getConstantPool(); |
| 610 | assert(ConstantPool && "Constant pool must be created" ); |
| 611 | if (initializeConstantPool(PFS, ConstantPool&: *ConstantPool, YamlMF)) |
| 612 | return true; |
| 613 | } |
| 614 | if (!YamlMF.MachineMetadataNodes.empty() && |
| 615 | parseMachineMetadataNodes(PFS, MF, YMF: YamlMF)) |
| 616 | return true; |
| 617 | |
| 618 | StringRef BlockStr = YamlMF.Body.Value.Value; |
| 619 | SMDiagnostic Error; |
| 620 | SourceMgr BlockSM; |
| 621 | BlockSM.AddNewSourceBuffer( |
| 622 | F: MemoryBuffer::getMemBuffer(InputData: BlockStr, BufferName: "" ,/*RequiresNullTerminator=*/false), |
| 623 | IncludeLoc: SMLoc()); |
| 624 | PFS.SM = &BlockSM; |
| 625 | if (parseMachineBasicBlockDefinitions(PFS, Src: BlockStr, Error)) { |
| 626 | reportDiagnostic( |
| 627 | Diag: diagFromBlockStringDiag(Error, SourceRange: YamlMF.Body.Value.SourceRange)); |
| 628 | return true; |
| 629 | } |
| 630 | // Check Basic Block Section Flags. |
| 631 | if (MF.hasBBSections()) { |
| 632 | MF.assignBeginEndSections(); |
| 633 | } |
| 634 | PFS.SM = &SM; |
| 635 | |
| 636 | // Initialize the frame information after creating all the MBBs so that the |
| 637 | // MBB references in the frame information can be resolved. |
| 638 | if (initializeFrameInfo(PFS, YamlMF)) |
| 639 | return true; |
| 640 | // Initialize the jump table after creating all the MBBs so that the MBB |
| 641 | // references can be resolved. |
| 642 | if (!YamlMF.JumpTableInfo.Entries.empty() && |
| 643 | initializeJumpTableInfo(PFS, YamlJTI: YamlMF.JumpTableInfo)) |
| 644 | return true; |
| 645 | // Parse the machine instructions after creating all of the MBBs so that the |
| 646 | // parser can resolve the MBB references. |
| 647 | StringRef InsnStr = YamlMF.Body.Value.Value; |
| 648 | SourceMgr InsnSM; |
| 649 | InsnSM.AddNewSourceBuffer( |
| 650 | F: MemoryBuffer::getMemBuffer(InputData: InsnStr, BufferName: "" , /*RequiresNullTerminator=*/false), |
| 651 | IncludeLoc: SMLoc()); |
| 652 | PFS.SM = &InsnSM; |
| 653 | if (parseMachineInstructions(PFS, Src: InsnStr, Error)) { |
| 654 | reportDiagnostic( |
| 655 | Diag: diagFromBlockStringDiag(Error, SourceRange: YamlMF.Body.Value.SourceRange)); |
| 656 | return true; |
| 657 | } |
| 658 | PFS.SM = &SM; |
| 659 | |
| 660 | if (setupRegisterInfo(PFS, YamlMF)) |
| 661 | return true; |
| 662 | |
| 663 | if (YamlMF.MachineFuncInfo) { |
| 664 | const TargetMachine &TM = MF.getTarget(); |
| 665 | // Note this is called after the initial constructor of the |
| 666 | // MachineFunctionInfo based on the MachineFunction, which may depend on the |
| 667 | // IR. |
| 668 | |
| 669 | SMRange SrcRange; |
| 670 | if (TM.parseMachineFunctionInfo(*YamlMF.MachineFuncInfo, PFS, Error, |
| 671 | SourceRange&: SrcRange)) { |
| 672 | return error(Error, SourceRange: SrcRange); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | // Set the reserved registers after parsing MachineFuncInfo. The target may |
| 677 | // have been recording information used to select the reserved registers |
| 678 | // there. |
| 679 | // FIXME: This is a temporary workaround until the reserved registers can be |
| 680 | // serialized. |
| 681 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 682 | MRI.freezeReservedRegs(); |
| 683 | |
| 684 | if (computeFunctionProperties(MF, YamlMF)) |
| 685 | return true; |
| 686 | |
| 687 | if (initializeCallSiteInfo(PFS, YamlMF)) |
| 688 | return true; |
| 689 | |
| 690 | if (parseCalledGlobals(PFS, MF, YMF: YamlMF)) |
| 691 | return true; |
| 692 | |
| 693 | if (initializePrefetchTargets(PFS, YamlMF)) |
| 694 | return true; |
| 695 | |
| 696 | setupDebugValueTracking(MF, PFS, YamlMF); |
| 697 | |
| 698 | MF.getSubtarget().mirFileLoaded(MF); |
| 699 | |
| 700 | MF.verify(p: nullptr, Banner: nullptr, OS: &errs()); |
| 701 | return false; |
| 702 | } |
| 703 | |
| 704 | bool MIRParserImpl::initializePrefetchTargets( |
| 705 | PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF) { |
| 706 | MachineFunction &MF = PFS.MF; |
| 707 | SMDiagnostic Error; |
| 708 | DenseMap<UniqueBBID, SmallVector<unsigned>> Targets; |
| 709 | for (const auto &YamlTarget : YamlMF.PrefetchTargets) { |
| 710 | CallsiteID Target; |
| 711 | if (llvm::parsePrefetchTarget(PFS, Target, Src: YamlTarget.Value, Error)) |
| 712 | return error(Error, SourceRange: YamlTarget.SourceRange); |
| 713 | Targets[Target.BBID].push_back(Elt: Target.CallsiteIndex); |
| 714 | } |
| 715 | MF.setPrefetchTargets(Targets); |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS, |
| 720 | const yaml::MachineFunction &YamlMF) { |
| 721 | MachineFunction &MF = PFS.MF; |
| 722 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 723 | assert(RegInfo.tracksLiveness()); |
| 724 | if (!YamlMF.TracksRegLiveness) |
| 725 | RegInfo.invalidateLiveness(); |
| 726 | |
| 727 | SMDiagnostic Error; |
| 728 | // Parse the virtual register information. |
| 729 | for (const auto &VReg : YamlMF.VirtualRegisters) { |
| 730 | VRegInfo &Info = PFS.getVRegInfo(Num: VReg.ID.Value); |
| 731 | if (Info.Explicit) |
| 732 | return error(Loc: VReg.ID.SourceRange.Start, |
| 733 | Message: Twine("redefinition of virtual register '%" ) + |
| 734 | Twine(VReg.ID.Value) + "'" ); |
| 735 | Info.Explicit = true; |
| 736 | |
| 737 | if (VReg.Class.Value == "_" ) { |
| 738 | Info.Kind = VRegInfo::GENERIC; |
| 739 | Info.D.RegBank = nullptr; |
| 740 | } else { |
| 741 | const auto *RC = Target->getRegClass(Name: VReg.Class.Value); |
| 742 | if (RC) { |
| 743 | Info.Kind = VRegInfo::NORMAL; |
| 744 | Info.D.RC = RC; |
| 745 | } else { |
| 746 | const RegisterBank *RegBank = Target->getRegBank(Name: VReg.Class.Value); |
| 747 | if (!RegBank) |
| 748 | return error( |
| 749 | Loc: VReg.Class.SourceRange.Start, |
| 750 | Message: Twine("use of undefined register class or register bank '" ) + |
| 751 | VReg.Class.Value + "'" ); |
| 752 | Info.Kind = VRegInfo::REGBANK; |
| 753 | Info.D.RegBank = RegBank; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (!VReg.PreferredRegister.Value.empty()) { |
| 758 | if (Info.Kind != VRegInfo::NORMAL) |
| 759 | return error(Loc: VReg.Class.SourceRange.Start, |
| 760 | Message: Twine("preferred register can only be set for normal vregs" )); |
| 761 | |
| 762 | if (parseRegisterReference(PFS, Reg&: Info.PreferredReg, |
| 763 | Src: VReg.PreferredRegister.Value, Error)) |
| 764 | return error(Error, SourceRange: VReg.PreferredRegister.SourceRange); |
| 765 | } |
| 766 | |
| 767 | for (const auto &FlagStringValue : VReg.RegisterFlags) { |
| 768 | uint8_t FlagValue; |
| 769 | if (Target->getVRegFlagValue(FlagName: FlagStringValue.Value, FlagValue)) |
| 770 | return error(Loc: FlagStringValue.SourceRange.Start, |
| 771 | Message: Twine("use of undefined register flag '" ) + |
| 772 | FlagStringValue.Value + "'" ); |
| 773 | Info.Flags |= FlagValue; |
| 774 | } |
| 775 | RegInfo.noteNewVirtualRegister(Reg: Info.VReg); |
| 776 | } |
| 777 | |
| 778 | // Parse the liveins. |
| 779 | for (const auto &LiveIn : YamlMF.LiveIns) { |
| 780 | Register Reg; |
| 781 | if (parseNamedRegisterReference(PFS, Reg, Src: LiveIn.Register.Value, Error)) |
| 782 | return error(Error, SourceRange: LiveIn.Register.SourceRange); |
| 783 | Register VReg; |
| 784 | if (!LiveIn.VirtualRegister.Value.empty()) { |
| 785 | VRegInfo *Info; |
| 786 | if (parseVirtualRegisterReference(PFS, Info, Src: LiveIn.VirtualRegister.Value, |
| 787 | Error)) |
| 788 | return error(Error, SourceRange: LiveIn.VirtualRegister.SourceRange); |
| 789 | VReg = Info->VReg; |
| 790 | } |
| 791 | RegInfo.addLiveIn(Reg, vreg: VReg); |
| 792 | } |
| 793 | |
| 794 | // Parse the callee saved registers (Registers that will |
| 795 | // be saved for the caller). |
| 796 | if (YamlMF.CalleeSavedRegisters) { |
| 797 | SmallVector<MCPhysReg, 16> CalleeSavedRegisters; |
| 798 | for (const auto &RegSource : *YamlMF.CalleeSavedRegisters) { |
| 799 | Register Reg; |
| 800 | if (parseNamedRegisterReference(PFS, Reg, Src: RegSource.Value, Error)) |
| 801 | return error(Error, SourceRange: RegSource.SourceRange); |
| 802 | CalleeSavedRegisters.push_back(Elt: Reg.id()); |
| 803 | } |
| 804 | RegInfo.setCalleeSavedRegs(CalleeSavedRegisters); |
| 805 | } |
| 806 | |
| 807 | return false; |
| 808 | } |
| 809 | |
| 810 | bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS, |
| 811 | const yaml::MachineFunction &YamlMF) { |
| 812 | MachineFunction &MF = PFS.MF; |
| 813 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 814 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 815 | |
| 816 | SmallVector<std::string> Errors; |
| 817 | |
| 818 | // Create VRegs |
| 819 | auto populateVRegInfo = [&](const VRegInfo &Info, const Twine &Name) { |
| 820 | Register Reg = Info.VReg; |
| 821 | switch (Info.Kind) { |
| 822 | case VRegInfo::UNKNOWN: |
| 823 | Errors.push_back( |
| 824 | Elt: (Twine("Cannot determine class/bank of virtual register " ) + Name + |
| 825 | " in function '" + MF.getName() + "'" ) |
| 826 | .str()); |
| 827 | break; |
| 828 | case VRegInfo::NORMAL: |
| 829 | if (!Info.D.RC->isAllocatable()) { |
| 830 | Errors.push_back(Elt: (Twine("Cannot use non-allocatable class '" ) + |
| 831 | TRI->getRegClassName(Class: Info.D.RC) + |
| 832 | "' for virtual register " + Name + " in function '" + |
| 833 | MF.getName() + "'" ) |
| 834 | .str()); |
| 835 | break; |
| 836 | } |
| 837 | |
| 838 | MRI.setRegClass(Reg, RC: Info.D.RC); |
| 839 | if (Info.PreferredReg != 0) |
| 840 | MRI.setSimpleHint(VReg: Reg, PrefReg: Info.PreferredReg); |
| 841 | break; |
| 842 | case VRegInfo::GENERIC: |
| 843 | break; |
| 844 | case VRegInfo::REGBANK: |
| 845 | MRI.setRegBank(Reg, RegBank: *Info.D.RegBank); |
| 846 | break; |
| 847 | } |
| 848 | }; |
| 849 | |
| 850 | for (const auto &P : PFS.VRegInfosNamed) { |
| 851 | const VRegInfo &Info = *P.second; |
| 852 | populateVRegInfo(Info, Twine(P.first())); |
| 853 | } |
| 854 | |
| 855 | for (auto P : PFS.VRegInfos) { |
| 856 | const VRegInfo &Info = *P.second; |
| 857 | populateVRegInfo(Info, Twine(P.first.id())); |
| 858 | } |
| 859 | |
| 860 | // Compute MachineRegisterInfo::UsedPhysRegMask |
| 861 | for (const MachineBasicBlock &MBB : MF) { |
| 862 | // Make sure MRI knows about registers clobbered by unwinder. |
| 863 | if (MBB.isEHPad()) |
| 864 | if (auto *RegMask = TRI->getCustomEHPadPreservedMask(MF)) |
| 865 | MRI.addPhysRegsUsedFromRegMask(RegMask); |
| 866 | |
| 867 | for (const MachineInstr &MI : MBB) { |
| 868 | for (const MachineOperand &MO : MI.operands()) { |
| 869 | if (!MO.isRegMask()) |
| 870 | continue; |
| 871 | MRI.addPhysRegsUsedFromRegMask(RegMask: MO.getRegMask()); |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | if (Errors.empty()) |
| 877 | return false; |
| 878 | |
| 879 | // Report errors in a deterministic order. |
| 880 | sort(C&: Errors); |
| 881 | for (auto &E : Errors) |
| 882 | error(Message: E); |
| 883 | return true; |
| 884 | } |
| 885 | |
| 886 | bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS, |
| 887 | const yaml::MachineFunction &YamlMF) { |
| 888 | MachineFunction &MF = PFS.MF; |
| 889 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 890 | const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); |
| 891 | const Function &F = MF.getFunction(); |
| 892 | const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo; |
| 893 | MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken); |
| 894 | MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken); |
| 895 | MFI.setHasStackMap(YamlMFI.HasStackMap); |
| 896 | MFI.setHasPatchPoint(YamlMFI.HasPatchPoint); |
| 897 | MFI.setStackSize(YamlMFI.StackSize); |
| 898 | MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment); |
| 899 | if (YamlMFI.MaxAlignment) |
| 900 | MFI.ensureMaxAlignment(Alignment: Align(YamlMFI.MaxAlignment)); |
| 901 | MFI.setAdjustsStack(YamlMFI.AdjustsStack); |
| 902 | MFI.setHasCalls(YamlMFI.HasCalls); |
| 903 | if (YamlMFI.MaxCallFrameSize != ~0u) |
| 904 | MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize); |
| 905 | MFI.setCVBytesOfCalleeSavedRegisters(YamlMFI.CVBytesOfCalleeSavedRegisters); |
| 906 | MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment); |
| 907 | MFI.setHasVAStart(YamlMFI.HasVAStart); |
| 908 | MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc); |
| 909 | MFI.setHasTailCall(YamlMFI.HasTailCall); |
| 910 | MFI.setCalleeSavedInfoValid(YamlMFI.IsCalleeSavedInfoValid); |
| 911 | MFI.setLocalFrameSize(YamlMFI.LocalFrameSize); |
| 912 | llvm::SaveRestorePoints SavePoints; |
| 913 | if (initializeSaveRestorePoints(PFS, YamlSRPoints: YamlMFI.SavePoints, SaveRestorePoints&: SavePoints)) |
| 914 | return true; |
| 915 | MFI.setSavePoints(SavePoints); |
| 916 | llvm::SaveRestorePoints RestorePoints; |
| 917 | if (initializeSaveRestorePoints(PFS, YamlSRPoints: YamlMFI.RestorePoints, SaveRestorePoints&: RestorePoints)) |
| 918 | return true; |
| 919 | MFI.setRestorePoints(RestorePoints); |
| 920 | |
| 921 | std::vector<CalleeSavedInfo> CSIInfo; |
| 922 | // Initialize the fixed frame objects. |
| 923 | for (const auto &Object : YamlMF.FixedStackObjects) { |
| 924 | int ObjectIdx; |
| 925 | if (Object.Type != yaml::FixedMachineStackObject::SpillSlot) |
| 926 | ObjectIdx = MFI.CreateFixedObject(Size: Object.Size, SPOffset: Object.Offset, |
| 927 | IsImmutable: Object.IsImmutable, isAliased: Object.IsAliased); |
| 928 | else |
| 929 | ObjectIdx = MFI.CreateFixedSpillStackObject(Size: Object.Size, SPOffset: Object.Offset); |
| 930 | |
| 931 | if (!TFI->isSupportedStackID(ID: Object.StackID)) |
| 932 | return error(Loc: Object.ID.SourceRange.Start, |
| 933 | Message: Twine("StackID is not supported by target" )); |
| 934 | MFI.setStackID(ObjectIdx, ID: Object.StackID); |
| 935 | MFI.setObjectAlignment(ObjectIdx, Alignment: Object.Alignment.valueOrOne()); |
| 936 | if (!PFS.FixedStackObjectSlots.insert(KV: std::make_pair(x: Object.ID.Value, |
| 937 | y&: ObjectIdx)) |
| 938 | .second) |
| 939 | return error(Loc: Object.ID.SourceRange.Start, |
| 940 | Message: Twine("redefinition of fixed stack object '%fixed-stack." ) + |
| 941 | Twine(Object.ID.Value) + "'" ); |
| 942 | if (parseCalleeSavedRegister(PFS, CSIInfo, RegisterSource: Object.CalleeSavedRegister, |
| 943 | IsRestored: Object.CalleeSavedRestored, FrameIdx: ObjectIdx)) |
| 944 | return true; |
| 945 | if (parseStackObjectsDebugInfo(PFS, Object, FrameIdx: ObjectIdx)) |
| 946 | return true; |
| 947 | } |
| 948 | |
| 949 | for (const auto &Object : YamlMF.EntryValueObjects) { |
| 950 | SMDiagnostic Error; |
| 951 | Register Reg; |
| 952 | if (parseNamedRegisterReference(PFS, Reg, Src: Object.EntryValueRegister.Value, |
| 953 | Error)) |
| 954 | return error(Error, SourceRange: Object.EntryValueRegister.SourceRange); |
| 955 | if (!Reg.isPhysical()) |
| 956 | return error(Loc: Object.EntryValueRegister.SourceRange.Start, |
| 957 | Message: "Expected physical register for entry value field" ); |
| 958 | std::optional<VarExprLoc> MaybeInfo = parseVarExprLoc( |
| 959 | PFS, VarStr: Object.DebugVar, ExprStr: Object.DebugExpr, LocStr: Object.DebugLoc); |
| 960 | if (!MaybeInfo) |
| 961 | return true; |
| 962 | if (MaybeInfo->DIVar || MaybeInfo->DIExpr || MaybeInfo->DILoc) |
| 963 | PFS.MF.setVariableDbgInfo(Var: MaybeInfo->DIVar, Expr: MaybeInfo->DIExpr, |
| 964 | Reg: Reg.asMCReg(), Loc: MaybeInfo->DILoc); |
| 965 | } |
| 966 | |
| 967 | // Initialize the ordinary frame objects. |
| 968 | for (const auto &Object : YamlMF.StackObjects) { |
| 969 | int ObjectIdx; |
| 970 | const AllocaInst *Alloca = nullptr; |
| 971 | const yaml::StringValue &Name = Object.Name; |
| 972 | if (!Name.Value.empty()) { |
| 973 | Alloca = dyn_cast_or_null<AllocaInst>( |
| 974 | Val: F.getValueSymbolTable()->lookup(Name: Name.Value)); |
| 975 | if (!Alloca) |
| 976 | return error(Loc: Name.SourceRange.Start, |
| 977 | Message: "alloca instruction named '" + Name.Value + |
| 978 | "' isn't defined in the function '" + F.getName() + |
| 979 | "'" ); |
| 980 | } |
| 981 | if (!TFI->isSupportedStackID(ID: Object.StackID)) |
| 982 | return error(Loc: Object.ID.SourceRange.Start, |
| 983 | Message: Twine("StackID is not supported by target" )); |
| 984 | if (Object.Type == yaml::MachineStackObject::VariableSized) |
| 985 | ObjectIdx = |
| 986 | MFI.CreateVariableSizedObject(Alignment: Object.Alignment.valueOrOne(), Alloca); |
| 987 | else |
| 988 | ObjectIdx = MFI.CreateStackObject( |
| 989 | Size: Object.Size, Alignment: Object.Alignment.valueOrOne(), |
| 990 | isSpillSlot: Object.Type == yaml::MachineStackObject::SpillSlot, Alloca, |
| 991 | ID: Object.StackID); |
| 992 | MFI.setObjectOffset(ObjectIdx, SPOffset: Object.Offset); |
| 993 | |
| 994 | if (!PFS.StackObjectSlots.insert(KV: std::make_pair(x: Object.ID.Value, y&: ObjectIdx)) |
| 995 | .second) |
| 996 | return error(Loc: Object.ID.SourceRange.Start, |
| 997 | Message: Twine("redefinition of stack object '%stack." ) + |
| 998 | Twine(Object.ID.Value) + "'" ); |
| 999 | if (parseCalleeSavedRegister(PFS, CSIInfo, RegisterSource: Object.CalleeSavedRegister, |
| 1000 | IsRestored: Object.CalleeSavedRestored, FrameIdx: ObjectIdx)) |
| 1001 | return true; |
| 1002 | if (Object.LocalOffset) |
| 1003 | MFI.mapLocalFrameObject(ObjectIndex: ObjectIdx, Offset: *Object.LocalOffset); |
| 1004 | if (parseStackObjectsDebugInfo(PFS, Object, FrameIdx: ObjectIdx)) |
| 1005 | return true; |
| 1006 | } |
| 1007 | MFI.setCalleeSavedInfo(CSIInfo); |
| 1008 | if (!CSIInfo.empty()) |
| 1009 | MFI.setCalleeSavedInfoValid(true); |
| 1010 | |
| 1011 | // Initialize the various stack object references after initializing the |
| 1012 | // stack objects. |
| 1013 | if (!YamlMFI.StackProtector.Value.empty()) { |
| 1014 | SMDiagnostic Error; |
| 1015 | int FI; |
| 1016 | if (parseStackObjectReference(PFS, FI, Src: YamlMFI.StackProtector.Value, Error)) |
| 1017 | return error(Error, SourceRange: YamlMFI.StackProtector.SourceRange); |
| 1018 | MFI.setStackProtectorIndex(FI); |
| 1019 | } |
| 1020 | |
| 1021 | if (!YamlMFI.FunctionContext.Value.empty()) { |
| 1022 | SMDiagnostic Error; |
| 1023 | int FI; |
| 1024 | if (parseStackObjectReference(PFS, FI, Src: YamlMFI.FunctionContext.Value, Error)) |
| 1025 | return error(Error, SourceRange: YamlMFI.FunctionContext.SourceRange); |
| 1026 | MFI.setFunctionContextIndex(FI); |
| 1027 | } |
| 1028 | |
| 1029 | return false; |
| 1030 | } |
| 1031 | |
| 1032 | bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS, |
| 1033 | std::vector<CalleeSavedInfo> &CSIInfo, |
| 1034 | const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) { |
| 1035 | if (RegisterSource.Value.empty()) |
| 1036 | return false; |
| 1037 | Register Reg; |
| 1038 | SMDiagnostic Error; |
| 1039 | if (parseNamedRegisterReference(PFS, Reg, Src: RegisterSource.Value, Error)) |
| 1040 | return error(Error, SourceRange: RegisterSource.SourceRange); |
| 1041 | CalleeSavedInfo CSI(Reg, FrameIdx); |
| 1042 | CSI.setRestored(IsRestored); |
| 1043 | CSIInfo.push_back(x: CSI); |
| 1044 | return false; |
| 1045 | } |
| 1046 | |
| 1047 | /// Verify that given node is of a certain type. Return true on error. |
| 1048 | template <typename T> |
| 1049 | static bool typecheckMDNode(T *&Result, MDNode *Node, |
| 1050 | const yaml::StringValue &Source, |
| 1051 | StringRef TypeString, MIRParserImpl &Parser) { |
| 1052 | if (!Node) |
| 1053 | return false; |
| 1054 | Result = dyn_cast<T>(Node); |
| 1055 | if (!Result) |
| 1056 | return Parser.error(Loc: Source.SourceRange.Start, |
| 1057 | Message: "expected a reference to a '" + TypeString + |
| 1058 | "' metadata node" ); |
| 1059 | return false; |
| 1060 | } |
| 1061 | |
| 1062 | std::optional<MIRParserImpl::VarExprLoc> MIRParserImpl::parseVarExprLoc( |
| 1063 | PerFunctionMIParsingState &PFS, const yaml::StringValue &VarStr, |
| 1064 | const yaml::StringValue &ExprStr, const yaml::StringValue &LocStr) { |
| 1065 | MDNode *Var = nullptr; |
| 1066 | MDNode *Expr = nullptr; |
| 1067 | MDNode *Loc = nullptr; |
| 1068 | if (parseMDNode(PFS, Node&: Var, Source: VarStr) || parseMDNode(PFS, Node&: Expr, Source: ExprStr) || |
| 1069 | parseMDNode(PFS, Node&: Loc, Source: LocStr)) |
| 1070 | return std::nullopt; |
| 1071 | DILocalVariable *DIVar = nullptr; |
| 1072 | DIExpression *DIExpr = nullptr; |
| 1073 | DILocation *DILoc = nullptr; |
| 1074 | if (typecheckMDNode(Result&: DIVar, Node: Var, Source: VarStr, TypeString: "DILocalVariable" , Parser&: *this) || |
| 1075 | typecheckMDNode(Result&: DIExpr, Node: Expr, Source: ExprStr, TypeString: "DIExpression" , Parser&: *this) || |
| 1076 | typecheckMDNode(Result&: DILoc, Node: Loc, Source: LocStr, TypeString: "DILocation" , Parser&: *this)) |
| 1077 | return std::nullopt; |
| 1078 | return VarExprLoc{.DIVar: DIVar, .DIExpr: DIExpr, .DILoc: DILoc}; |
| 1079 | } |
| 1080 | |
| 1081 | template <typename T> |
| 1082 | bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS, |
| 1083 | const T &Object, int FrameIdx) { |
| 1084 | std::optional<VarExprLoc> MaybeInfo = |
| 1085 | parseVarExprLoc(PFS, VarStr: Object.DebugVar, ExprStr: Object.DebugExpr, LocStr: Object.DebugLoc); |
| 1086 | if (!MaybeInfo) |
| 1087 | return true; |
| 1088 | // Debug information can only be attached to stack objects; Fixed stack |
| 1089 | // objects aren't supported. |
| 1090 | if (MaybeInfo->DIVar || MaybeInfo->DIExpr || MaybeInfo->DILoc) |
| 1091 | PFS.MF.setVariableDbgInfo(Var: MaybeInfo->DIVar, Expr: MaybeInfo->DIExpr, Slot: FrameIdx, |
| 1092 | Loc: MaybeInfo->DILoc); |
| 1093 | return false; |
| 1094 | } |
| 1095 | |
| 1096 | bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS, |
| 1097 | MDNode *&Node, const yaml::StringValue &Source) { |
| 1098 | if (Source.Value.empty()) |
| 1099 | return false; |
| 1100 | SMDiagnostic Error; |
| 1101 | if (llvm::parseMDNode(PFS, Node, Src: Source.Value, Error)) |
| 1102 | return error(Error, SourceRange: Source.SourceRange); |
| 1103 | return false; |
| 1104 | } |
| 1105 | |
| 1106 | bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS, |
| 1107 | MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) { |
| 1108 | DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots; |
| 1109 | const MachineFunction &MF = PFS.MF; |
| 1110 | const auto &M = *MF.getFunction().getParent(); |
| 1111 | SMDiagnostic Error; |
| 1112 | for (const auto &YamlConstant : YamlMF.Constants) { |
| 1113 | if (YamlConstant.IsTargetSpecific) |
| 1114 | // FIXME: Support target-specific constant pools |
| 1115 | return error(Loc: YamlConstant.Value.SourceRange.Start, |
| 1116 | Message: "Can't parse target-specific constant pool entries yet" ); |
| 1117 | const Constant *Value = dyn_cast_or_null<Constant>( |
| 1118 | Val: parseConstantValue(Asm: YamlConstant.Value.Value, Err&: Error, M)); |
| 1119 | if (!Value) |
| 1120 | return error(Error, SourceRange: YamlConstant.Value.SourceRange); |
| 1121 | const Align PrefTypeAlign = |
| 1122 | M.getDataLayout().getPrefTypeAlign(Ty: Value->getType()); |
| 1123 | const Align Alignment = YamlConstant.Alignment.value_or(u: PrefTypeAlign); |
| 1124 | unsigned Index = ConstantPool.getConstantPoolIndex(C: Value, Alignment); |
| 1125 | if (!ConstantPoolSlots.insert(KV: std::make_pair(x: YamlConstant.ID.Value, y&: Index)) |
| 1126 | .second) |
| 1127 | return error(Loc: YamlConstant.ID.SourceRange.Start, |
| 1128 | Message: Twine("redefinition of constant pool item '%const." ) + |
| 1129 | Twine(YamlConstant.ID.Value) + "'" ); |
| 1130 | } |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
| 1134 | // Return true if basic block was incorrectly specified in MIR |
| 1135 | bool MIRParserImpl::initializeSaveRestorePoints( |
| 1136 | PerFunctionMIParsingState &PFS, |
| 1137 | const std::vector<yaml::SaveRestorePointEntry> &YamlSRPoints, |
| 1138 | llvm::SaveRestorePoints &SaveRestorePoints) { |
| 1139 | SMDiagnostic Error; |
| 1140 | MachineBasicBlock *MBB = nullptr; |
| 1141 | for (const yaml::SaveRestorePointEntry &Entry : YamlSRPoints) { |
| 1142 | if (parseMBBReference(PFS, MBB, Source: Entry.Point.Value)) |
| 1143 | return true; |
| 1144 | |
| 1145 | std::vector<CalleeSavedInfo> Registers; |
| 1146 | for (auto &RegStr : Entry.Registers) { |
| 1147 | Register Reg; |
| 1148 | if (parseNamedRegisterReference(PFS, Reg, Src: RegStr.Value, Error)) |
| 1149 | return error(Error, SourceRange: RegStr.SourceRange); |
| 1150 | Registers.push_back(x: CalleeSavedInfo(Reg)); |
| 1151 | } |
| 1152 | SaveRestorePoints.try_emplace(Key: MBB, Args: std::move(Registers)); |
| 1153 | } |
| 1154 | return false; |
| 1155 | } |
| 1156 | |
| 1157 | bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS, |
| 1158 | const yaml::MachineJumpTable &YamlJTI) { |
| 1159 | MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(JTEntryKind: YamlJTI.Kind); |
| 1160 | for (const auto &Entry : YamlJTI.Entries) { |
| 1161 | std::vector<MachineBasicBlock *> Blocks; |
| 1162 | for (const auto &MBBSource : Entry.Blocks) { |
| 1163 | MachineBasicBlock *MBB = nullptr; |
| 1164 | if (parseMBBReference(PFS, MBB, Source: MBBSource.Value)) |
| 1165 | return true; |
| 1166 | Blocks.push_back(x: MBB); |
| 1167 | } |
| 1168 | unsigned Index = JTI->createJumpTableIndex(DestBBs: Blocks); |
| 1169 | if (!PFS.JumpTableSlots.insert(KV: std::make_pair(x: Entry.ID.Value, y&: Index)) |
| 1170 | .second) |
| 1171 | return error(Loc: Entry.ID.SourceRange.Start, |
| 1172 | Message: Twine("redefinition of jump table entry '%jump-table." ) + |
| 1173 | Twine(Entry.ID.Value) + "'" ); |
| 1174 | } |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
| 1178 | bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS, |
| 1179 | MachineBasicBlock *&MBB, |
| 1180 | const yaml::StringValue &Source) { |
| 1181 | SMDiagnostic Error; |
| 1182 | if (llvm::parseMBBReference(PFS, MBB, Src: Source.Value, Error)) |
| 1183 | return error(Error, SourceRange: Source.SourceRange); |
| 1184 | return false; |
| 1185 | } |
| 1186 | |
| 1187 | bool MIRParserImpl::parseMachineMetadata(PerFunctionMIParsingState &PFS, |
| 1188 | const yaml::StringValue &Source) { |
| 1189 | SMDiagnostic Error; |
| 1190 | if (llvm::parseMachineMetadata(PFS, Src: Source.Value, SourceRange: Source.SourceRange, Error)) |
| 1191 | return error(Error, SourceRange: Source.SourceRange); |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
| 1195 | bool MIRParserImpl::parseMachineMetadataNodes( |
| 1196 | PerFunctionMIParsingState &PFS, MachineFunction &MF, |
| 1197 | const yaml::MachineFunction &YMF) { |
| 1198 | for (const auto &MDS : YMF.MachineMetadataNodes) { |
| 1199 | if (parseMachineMetadata(PFS, Source: MDS)) |
| 1200 | return true; |
| 1201 | } |
| 1202 | // Report missing definitions from forward referenced nodes. |
| 1203 | if (!PFS.MachineForwardRefMDNodes.empty()) |
| 1204 | return error(Loc: PFS.MachineForwardRefMDNodes.begin()->second.second, |
| 1205 | Message: "use of undefined metadata '!" + |
| 1206 | Twine(PFS.MachineForwardRefMDNodes.begin()->first) + "'" ); |
| 1207 | return false; |
| 1208 | } |
| 1209 | |
| 1210 | bool MIRParserImpl::parseCalledGlobals(PerFunctionMIParsingState &PFS, |
| 1211 | MachineFunction &MF, |
| 1212 | const yaml::MachineFunction &YMF) { |
| 1213 | Function &F = MF.getFunction(); |
| 1214 | for (const auto &YamlCG : YMF.CalledGlobals) { |
| 1215 | yaml::MachineInstrLoc MILoc = YamlCG.CallSite; |
| 1216 | const MachineInstr *CallI; |
| 1217 | if (parseMachineInst(MF, MILoc, MI&: CallI)) |
| 1218 | return true; |
| 1219 | if (!CallI->isCall(Type: MachineInstr::IgnoreBundle)) |
| 1220 | return error(Message: Twine(MF.getName()) + |
| 1221 | Twine(" called global should reference call " |
| 1222 | "instruction. Instruction at bb:" ) + |
| 1223 | Twine(MILoc.BlockNum) + " at offset:" + Twine(MILoc.Offset) + |
| 1224 | " is not a call instruction" ); |
| 1225 | |
| 1226 | auto Callee = |
| 1227 | F.getParent()->getValueSymbolTable().lookup(Name: YamlCG.Callee.Value); |
| 1228 | if (!Callee) |
| 1229 | return error(Loc: YamlCG.Callee.SourceRange.Start, |
| 1230 | Message: "use of undefined global '" + YamlCG.Callee.Value + "'" ); |
| 1231 | if (!isa<GlobalValue>(Val: Callee)) |
| 1232 | return error(Loc: YamlCG.Callee.SourceRange.Start, |
| 1233 | Message: "use of non-global value '" + YamlCG.Callee.Value + "'" ); |
| 1234 | |
| 1235 | MF.addCalledGlobal(MI: CallI, Details: {.Callee: cast<GlobalValue>(Val: Callee), .TargetFlags: YamlCG.Flags}); |
| 1236 | } |
| 1237 | |
| 1238 | return false; |
| 1239 | } |
| 1240 | |
| 1241 | SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error, |
| 1242 | SMRange SourceRange) { |
| 1243 | assert(SourceRange.isValid() && "Invalid source range" ); |
| 1244 | SMLoc Loc = SourceRange.Start; |
| 1245 | bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() && |
| 1246 | *Loc.getPointer() == '\''; |
| 1247 | // Translate the location of the error from the location in the MI string to |
| 1248 | // the corresponding location in the MIR file. |
| 1249 | Loc = Loc.getFromPointer(Ptr: Loc.getPointer() + Error.getColumnNo() + |
| 1250 | (HasQuote ? 1 : 0)); |
| 1251 | |
| 1252 | // TODO: Translate any source ranges as well. |
| 1253 | return SM.GetMessage(Loc, Kind: Error.getKind(), Msg: Error.getMessage(), Ranges: {}, |
| 1254 | FixIts: Error.getFixIts()); |
| 1255 | } |
| 1256 | |
| 1257 | SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error, |
| 1258 | SMRange SourceRange) { |
| 1259 | assert(SourceRange.isValid()); |
| 1260 | |
| 1261 | // Translate the location of the error from the location in the llvm IR string |
| 1262 | // to the corresponding location in the MIR file. |
| 1263 | auto LineAndColumn = SM.getLineAndColumn(Loc: SourceRange.Start); |
| 1264 | unsigned Line = LineAndColumn.first + Error.getLineNo() - 1; |
| 1265 | unsigned Column = Error.getColumnNo(); |
| 1266 | StringRef LineStr = Error.getLineContents(); |
| 1267 | SMLoc Loc = Error.getLoc(); |
| 1268 | |
| 1269 | // Get the full line and adjust the column number by taking the indentation of |
| 1270 | // LLVM IR into account. |
| 1271 | for (line_iterator L(*SM.getMemoryBuffer(i: SM.getMainFileID()), false), E; |
| 1272 | L != E; ++L) { |
| 1273 | if (L.line_number() == Line) { |
| 1274 | LineStr = *L; |
| 1275 | Loc = SMLoc::getFromPointer(Ptr: LineStr.data()); |
| 1276 | auto Indent = LineStr.find(Str: Error.getLineContents()); |
| 1277 | if (Indent != StringRef::npos) |
| 1278 | Column += Indent; |
| 1279 | break; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(), |
| 1284 | Error.getMessage(), LineStr, Error.getRanges(), |
| 1285 | Error.getFixIts()); |
| 1286 | } |
| 1287 | |
| 1288 | MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl) |
| 1289 | : Impl(std::move(Impl)) {} |
| 1290 | |
| 1291 | MIRParser::~MIRParser() = default; |
| 1292 | |
| 1293 | std::unique_ptr<Module> |
| 1294 | MIRParser::parseIRModule(DataLayoutCallbackTy DataLayoutCallback) { |
| 1295 | return Impl->parseIRModule(DataLayoutCallback); |
| 1296 | } |
| 1297 | |
| 1298 | bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) { |
| 1299 | return Impl->parseMachineFunctions(M, MMI); |
| 1300 | } |
| 1301 | |
| 1302 | bool MIRParser::parseMachineFunctions(Module &M, ModuleAnalysisManager &MAM) { |
| 1303 | auto &MMI = MAM.getResult<MachineModuleAnalysis>(IR&: M).getMMI(); |
| 1304 | return Impl->parseMachineFunctions(M, MMI, MAM: &MAM); |
| 1305 | } |
| 1306 | |
| 1307 | std::unique_ptr<MIRParser> llvm::createMIRParserFromFile( |
| 1308 | StringRef Filename, SMDiagnostic &Error, LLVMContext &Context, |
| 1309 | std::function<void(Function &)> ProcessIRFunction) { |
| 1310 | auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true); |
| 1311 | if (std::error_code EC = FileOrErr.getError()) { |
| 1312 | Error = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 1313 | "could not open input file: " + EC.message()); |
| 1314 | return nullptr; |
| 1315 | } |
| 1316 | return createMIRParser(Contents: std::move(FileOrErr.get()), Context, |
| 1317 | ProcessIRFunction); |
| 1318 | } |
| 1319 | |
| 1320 | std::unique_ptr<MIRParser> |
| 1321 | llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents, |
| 1322 | LLVMContext &Context, |
| 1323 | std::function<void(Function &)> ProcessIRFunction) { |
| 1324 | auto Filename = Contents->getBufferIdentifier(); |
| 1325 | if (Context.shouldDiscardValueNames()) { |
| 1326 | Context.diagnose(DI: DiagnosticInfoMIRParser( |
| 1327 | DS_Error, |
| 1328 | SMDiagnostic( |
| 1329 | Filename, SourceMgr::DK_Error, |
| 1330 | "cannot read MIR with a Context that discards named Values" ))); |
| 1331 | return nullptr; |
| 1332 | } |
| 1333 | return std::make_unique<MIRParser>(args: std::make_unique<MIRParserImpl>( |
| 1334 | args: std::move(Contents), args&: Filename, args&: Context, args&: ProcessIRFunction)); |
| 1335 | } |
| 1336 | |