src/irgenerator/MetadataGenerator.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "MetadataGenerator.h" | ||
| 4 | |||
| 5 | #include <global/TypeRegistry.h> | ||
| 6 | #include <irgenerator/IRGenerator.h> | ||
| 7 | |||
| 8 | #include <llvm/IR/Module.h> | ||
| 9 | |||
| 10 | namespace spice::compiler { | ||
| 11 | |||
| 12 | 987 | MetadataGenerator::MetadataGenerator(IRGenerator *irGenerator) | |
| 13 |
2/4✓ Branch 3 → 4 taken 987 times.
✗ Branch 3 → 8 not taken.
✓ Branch 4 → 5 taken 987 times.
✗ Branch 4 → 8 not taken.
|
987 | : irGenerator(irGenerator), mdBuilder(irGenerator->context), tbaaRoot(mdBuilder.createTBAARoot("Simple Spice TBAA")), |
| 14 |
2/4✓ Branch 5 → 6 taken 987 times.
✗ Branch 5 → 9 not taken.
✓ Branch 6 → 7 taken 987 times.
✗ Branch 6 → 9 not taken.
|
987 | omnipotentByte(mdBuilder.createTBAAScalarTypeNode("omnipotent byte", tbaaRoot)) {} |
| 15 | |||
| 16 | 1226 | void MetadataGenerator::generateBranchWeightsMetadata(llvm::BranchInst *jumpInst, Likelihood likeliness) { | |
| 17 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1226 times.
|
1226 | assert(likeliness != Likelihood::UNSPECIFIED); |
| 18 | 1226 | const bool likely = likeliness == Likelihood::LIKELY; | |
| 19 |
1/2✓ Branch 4 → 5 taken 1226 times.
✗ Branch 4 → 7 not taken.
|
1226 | llvm::MDNode *profMetadata = likely ? mdBuilder.createLikelyBranchWeights() : mdBuilder.createUnlikelyBranchWeights(); |
| 20 | 1226 | jumpInst->setMetadata(llvm::LLVMContext::MD_prof, profMetadata); | |
| 21 | 1226 | } | |
| 22 | |||
| 23 | 9 | void MetadataGenerator::generateTypeMetadata(llvm::Instruction *inst, const QualType &type) { | |
| 24 | 9 | const uint64_t typeHash = TypeRegistry::getTypeHash(*type.getType()); | |
| 25 | |||
| 26 |
2/4✓ Branch 4 → 5 taken 9 times.
✗ Branch 4 → 16 not taken.
✓ Branch 6 → 7 taken 9 times.
✗ Branch 6 → 14 not taken.
|
9 | llvm::MDString *typeName = mdBuilder.createString(type.getName(true, true)); |
| 27 | 9 | llvm::ConstantAsMetadata *typeId = mdBuilder.createConstant(irGenerator->builder.getInt64(typeHash)); | |
| 28 |
1/2✓ Branch 11 → 12 taken 9 times.
✗ Branch 11 → 18 not taken.
|
9 | llvm::MDNode *typeMetadata = llvm::MDNode::get(irGenerator->context, {typeId, typeName}); |
| 29 | |||
| 30 | 9 | inst->setMetadata(llvm::LLVMContext::MD_type, typeMetadata); | |
| 31 | 9 | } | |
| 32 | |||
| 33 | 15 | void MetadataGenerator::generateTBAAMetadata(llvm::Instruction *inst, const QualType &type) { | |
| 34 |
1/2✓ Branch 2 → 3 taken 15 times.
✗ Branch 2 → 17 not taken.
|
15 | const std::string typeName = type.getName(true, true); |
| 35 |
1/2✓ Branch 4 → 5 taken 15 times.
✗ Branch 4 → 12 not taken.
|
15 | llvm::MDNode *tbaaTypeNode = mdBuilder.createTBAAScalarTypeNode(typeName, omnipotentByte); |
| 36 | // Seems so new, that the verifier does not accept it | ||
| 37 | //const llvm::TypeSize typeSize = irGenerator->module->getDataLayout().getTypeAllocSize(llvmType); | ||
| 38 | //const bool isImmutable = type.isConst(); | ||
| 39 | //llvm::MDNode *tbaaAccessTag = mdBuilder.createTBAAAccessTag(tbaaTypeNode, tbaaTypeNode, 0, typeSize, isImmutable); | ||
| 40 |
2/4✓ Branch 5 → 6 taken 15 times.
✗ Branch 5 → 15 not taken.
✓ Branch 6 → 7 taken 15 times.
✗ Branch 6 → 15 not taken.
|
15 | llvm::ConstantAsMetadata *offset = llvm::ConstantAsMetadata::get(irGenerator->builder.getInt64(0)); |
| 41 |
1/2✓ Branch 8 → 9 taken 15 times.
✗ Branch 8 → 13 not taken.
|
15 | llvm::MDNode *tbaaAccessTag = llvm::MDNode::get(irGenerator->context, {tbaaTypeNode, tbaaTypeNode, offset}); |
| 42 | |||
| 43 |
1/2✓ Branch 9 → 10 taken 15 times.
✗ Branch 9 → 15 not taken.
|
15 | inst->setMetadata(llvm::LLVMContext::MD_tbaa, tbaaAccessTag); |
| 44 | 15 | } | |
| 45 | |||
| 46 | } // namespace spice::compiler | ||
| 47 |