Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ecf591c
Update CurLineNum anc CurColNum in sync with movement in text
Bertik23 Jul 29, 2025
06926e9
Remove remains from cherry pick from LSP branch
Bertik23 Aug 4, 2025
1fdf13c
Make isLabelTail more safe and rename it to better show what it does
Bertik23 Aug 4, 2025
2772cd8
Remove dangling comment
Bertik23 Aug 4, 2025
b05d11a
Fix typo
Bertik23 Aug 12, 2025
458599b
Add location tracking to IR parser
Bertik23 Aug 28, 2025
ee39ed1
Fix some OOB posibilities
Bertik23 Aug 28, 2025
b0c5318
Fix clang format
Bertik23 Aug 28, 2025
416514e
Move private members to top of class definition
Bertik23 Aug 29, 2025
35ca1a5
Use SourceMgr to resolve Line:Column position
Bertik23 Sep 2, 2025
b3d8254
Fix zeroindexing on token positions
Bertik23 Sep 2, 2025
23dcc6b
Replace Line:Column storage with Poiters and on demand conversion
Bertik23 Sep 3, 2025
06d5265
Use nullptr as missing value
Bertik23 Sep 4, 2025
4e08921
Enclose debug prints of tests in LLVM_DEBUG
Bertik23 Sep 4, 2025
3da9e9d
Decapitalize DEBUG_TYPE
Bertik23 Sep 15, 2025
4b3bc0e
Move FileLoc from Value.h to FileLoc.h
Bertik23 Sep 26, 2025
ed7a04a
Rename include guard defines to reflext filename
Bertik23 Sep 26, 2025
f9728f6
Merge remote-tracking branch 'upstream/main' into lsp-server-upstreaming
Bertik23 Oct 1, 2025
4a749d2
LSP server
Bertik23 Oct 1, 2025
d870571
Update LLVMBuild.txt in accordance to CMakeLists.txt
Bertik23 Oct 1, 2025
53bf014
Update LSP capabilities in README
Bertik23 Oct 1, 2025
bd6d352
Merge branch 'llvm:main' into lsp-server-upstreaming
Bertik23 Oct 1, 2025
5aef2db
Fix clang-format
Bertik23 Oct 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions llvm/include/llvm/Analysis/CFGPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include "llvm/Support/DOTGraphTraits.h"
#include "llvm/Support/FormatVariadic.h"

#include <functional>
#include <sstream>

namespace llvm {
class ModuleSlotTracker;

Expand Down Expand Up @@ -69,13 +72,18 @@ class DOTFuncInfo {
bool ShowHeat;
bool EdgeWeights;
bool RawWeights;
using NodeIdFormatterTy =
std::function<std::optional<std::string>(const BasicBlock *)>;
std::optional<NodeIdFormatterTy> NodeIdFormatter;

public:
DOTFuncInfo(const Function *F) : DOTFuncInfo(F, nullptr, nullptr, 0) {}
LLVM_ABI ~DOTFuncInfo();

LLVM_ABI DOTFuncInfo(const Function *F, const BlockFrequencyInfo *BFI,
const BranchProbabilityInfo *BPI, uint64_t MaxFreq);
LLVM_ABI
DOTFuncInfo(const Function *F, const BlockFrequencyInfo *BFI,
const BranchProbabilityInfo *BPI, uint64_t MaxFreq,
std::optional<NodeIdFormatterTy> NodeIdFormatter = std::nullopt);

const BlockFrequencyInfo *getBFI() const { return BFI; }

Expand All @@ -102,6 +110,10 @@ class DOTFuncInfo {
void setEdgeWeights(bool EdgeWeights) { this->EdgeWeights = EdgeWeights; }

bool showEdgeWeights() { return EdgeWeights; }

std::optional<NodeIdFormatterTy> getNodeIdFormatter() {
return NodeIdFormatter;
}
};

template <>
Expand Down Expand Up @@ -311,21 +323,27 @@ struct DOTGraphTraits<DOTFuncInfo *> : public DefaultDOTGraphTraits {
}

std::string getNodeAttributes(const BasicBlock *Node, DOTFuncInfo *CFGInfo) {
std::stringstream Attrs;

if (auto NodeIdFmt = CFGInfo->getNodeIdFormatter())
if (auto NodeId = (*NodeIdFmt)(Node))
Attrs << "id=\"" << *NodeId << "\"";

if (CFGInfo->showHeatColors()) {
uint64_t Freq = CFGInfo->getFreq(Node);
std::string Color = getHeatColor(Freq, CFGInfo->getMaxFreq());
std::string EdgeColor = (Freq <= (CFGInfo->getMaxFreq() / 2))
? (getHeatColor(0))
: (getHeatColor(1));
if (!Attrs.str().empty())
Attrs << ",";
Attrs << "color=\"" << EdgeColor << "ff\",style=filled," << "fillcolor=\""
<< Color << "70\"" << "fontname=\"Courier\"";
}

if (!CFGInfo->showHeatColors())
return "";

uint64_t Freq = CFGInfo->getFreq(Node);
std::string Color = getHeatColor(Freq, CFGInfo->getMaxFreq());
std::string EdgeColor = (Freq <= (CFGInfo->getMaxFreq() / 2))
? (getHeatColor(0))
: (getHeatColor(1));

std::string Attrs = "color=\"" + EdgeColor + "ff\", style=filled," +
" fillcolor=\"" + Color + "70\"" +
" fontname=\"Courier\"";
return Attrs;
return Attrs.str();
}

LLVM_ABI bool isNodeHidden(const BasicBlock *Node,
const DOTFuncInfo *CFGInfo);
LLVM_ABI void computeDeoptOrUnreachablePaths(const Function *F);
Expand Down
70 changes: 70 additions & 0 deletions llvm/include/llvm/AsmParser/AsmParserContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===-- AsmParserContext.h --------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ASMPARSER_ASMPARSERCONTEXT_H
#define LLVM_ASMPARSER_ASMPARSERCONTEXT_H

#include "FileLoc.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/IR/Value.h"
#include <optional>

namespace llvm {

/// Registry of file location information for LLVM IR constructs
///
/// This class provides access to the file location information
/// for various LLVM IR constructs. Currently, it supports Function,
/// BasicBlock and Instruction locations.
///
/// When available, it can answer queries about what is at a given
/// file location, as well as where in a file a given IR construct
/// is.
///
/// This information is optionally emitted by the LLParser while
/// it reads LLVM textual IR.
class AsmParserContext {
DenseMap<Function *, FileLocRange> Functions;
DenseMap<BasicBlock *, FileLocRange> Blocks;
DenseMap<Instruction *, FileLocRange> Instructions;

public:
std::optional<FileLocRange> getFunctionLocation(const Function *) const;
std::optional<FileLocRange> getBlockLocation(const BasicBlock *) const;
std::optional<FileLocRange> getInstructionLocation(const Instruction *) const;
/// Get the function at the requested location range.
/// If no single function occupies the queried range, or the record is
/// missing, a nullptr is returned.
Function *getFunctionAtLocation(const FileLocRange &) const;
/// Get the function at the requested location.
/// If no function occupies the queried location, or the record is missing, a
/// nullptr is returned.
Function *getFunctionAtLocation(const FileLoc &) const;
/// Get the block at the requested location range.
/// If no single block occupies the queried range, or the record is missing, a
/// nullptr is returned.
BasicBlock *getBlockAtLocation(const FileLocRange &) const;
/// Get the block at the requested location.
/// If no block occupies the queried location, or the record is missing, a
/// nullptr is returned.
BasicBlock *getBlockAtLocation(const FileLoc &) const;
/// Get the instruction at the requested location range.
/// If no single instruction occupies the queried range, or the record is
/// missing, a nullptr is returned.
Instruction *getInstructionAtLocation(const FileLocRange &) const;
/// Get the instruction at the requested location.
/// If no instruction occupies the queried location, or the record is missing,
/// a nullptr is returned.
Instruction *getInstructionAtLocation(const FileLoc &) const;
bool addFunctionLocation(Function *, const FileLocRange &);
bool addBlockLocation(BasicBlock *, const FileLocRange &);
bool addInstructionLocation(Instruction *, const FileLocRange &);
};
} // namespace llvm

#endif
52 changes: 52 additions & 0 deletions llvm/include/llvm/AsmParser/FileLoc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===-- FileLoc.h ---------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ASMPARSER_FILELOC_H
#define LLVM_ASMPARSER_FILELOC_H

#include <assert.h>
#include <utility>

namespace llvm {

struct FileLoc {
unsigned Line;
unsigned Col;

bool operator<=(const FileLoc &RHS) const {
return Line < RHS.Line || (Line == RHS.Line && Col <= RHS.Col);
}

bool operator<(const FileLoc &RHS) const {
return Line < RHS.Line || (Line == RHS.Line && Col < RHS.Col);
}

FileLoc(unsigned L, unsigned C) : Line(L), Col(C) {}
FileLoc(std::pair<unsigned, unsigned> LC) : Line(LC.first), Col(LC.second) {}
};

struct FileLocRange {
FileLoc Start;
FileLoc End;

FileLocRange() : Start(0, 0), End(0, 0) {}

FileLocRange(FileLoc S, FileLoc E) : Start(S), End(E) {
assert(Start <= End);
}

bool contains(FileLoc L) const { return Start <= L && L <= End; }

bool contains(FileLocRange LR) const {
return contains(LR.Start) && contains(LR.End);
}
};

} // namespace llvm

#endif
31 changes: 27 additions & 4 deletions llvm/include/llvm/AsmParser/LLLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/SourceMgr.h"
#include <string>

namespace llvm {
class Type;
class SMDiagnostic;
class SourceMgr;
class LLVMContext;

class LLLexer {
const char *CurPtr;
StringRef CurBuf;

// The the end (exclusive) of the current token
const char *PrevTokEnd = nullptr;

enum class ErrorPriority {
None, // No error message present.
Parser, // Errors issued by parser.
Expand Down Expand Up @@ -62,9 +65,7 @@ namespace llvm {
explicit LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &,
LLVMContext &C);

lltok::Kind Lex() {
return CurKind = LexToken();
}
lltok::Kind Lex() { return CurKind = LexToken(); }

typedef SMLoc LocTy;
LocTy getLoc() const { return SMLoc::getFromPointer(TokStart); }
Expand All @@ -79,6 +80,23 @@ namespace llvm {
IgnoreColonInIdentifiers = val;
}

// Get the line, column position of the start of the current token,
// zero-indexed
std::pair<unsigned, unsigned> getTokLineColumnPos() {
auto LC = SM.getLineAndColumn(SMLoc::getFromPointer(TokStart));
--LC.first;
--LC.second;
return LC;
}
// Get the line, column position of the end of the previous token,
// zero-indexed exclusive
std::pair<unsigned, unsigned> getPrevTokEndLineColumnPos() {
auto LC = SM.getLineAndColumn(SMLoc::getFromPointer(PrevTokEnd));
--LC.first;
--LC.second;
return LC;
}

// This returns true as a convenience for the parser functions that return
// true on error.
bool ParseError(LocTy ErrorLoc, const Twine &Msg) {
Expand All @@ -93,7 +111,12 @@ namespace llvm {
private:
lltok::Kind LexToken();

// Return closest pointer after `Ptr` that is an end of a label.
// Returns nullptr if `Ptr` doesn't point into a label.
const char *getLabelTail(const char *Ptr);
int getNextChar();
const char *skipNChars(unsigned N);
void advancePositionTo(const char *Ptr);
void SkipLineComment();
bool SkipCComment();
lltok::Kind ReadString(lltok::Kind kind);
Expand Down
9 changes: 7 additions & 2 deletions llvm/include/llvm/AsmParser/LLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_ASMPARSER_LLPARSER_H
#define LLVM_ASMPARSER_LLPARSER_H

#include "AsmParserContext.h"
#include "LLLexer.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/AsmParser/NumberedValues.h"
Expand Down Expand Up @@ -177,6 +178,9 @@ namespace llvm {
// Map of module ID to path.
std::map<unsigned, StringRef> ModuleIdMap;

/// Keeps track of source locations for Values, BasicBlocks, and Functions
AsmParserContext *ParserContext;

/// Only the llvm-as tool may set this to false to bypass
/// UpgradeDebuginfo so it can generate broken bitcode.
bool UpgradeDebugInfo;
Expand All @@ -189,10 +193,11 @@ namespace llvm {
public:
LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M,
ModuleSummaryIndex *Index, LLVMContext &Context,
SlotMapping *Slots = nullptr)
SlotMapping *Slots = nullptr,
AsmParserContext *ParserContext = nullptr)
: Context(Context), OPLex(F, SM, Err, Context),
Lex(F, SM, Err, Context), M(M), Index(Index), Slots(Slots),
BlockAddressPFS(nullptr) {}
BlockAddressPFS(nullptr), ParserContext(ParserContext) {}
bool Run(
bool UpgradeDebugInfo,
DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
Expand Down
16 changes: 9 additions & 7 deletions llvm/include/llvm/AsmParser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/AsmParser/AsmParserContext.h"
#include "llvm/Support/Compiler.h"
#include <memory>
#include <optional>
Expand Down Expand Up @@ -62,7 +63,8 @@ parseAssemblyFile(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
/// parsing.
LLVM_ABI std::unique_ptr<Module>
parseAssemblyString(StringRef AsmString, SMDiagnostic &Err,
LLVMContext &Context, SlotMapping *Slots = nullptr);
LLVMContext &Context, SlotMapping *Slots = nullptr,
AsmParserContext *ParserContext = nullptr);

/// Holds the Module and ModuleSummaryIndex returned by the interfaces
/// that parse both.
Expand Down Expand Up @@ -128,9 +130,9 @@ parseSummaryIndexAssemblyString(StringRef AsmString, SMDiagnostic &Err);
LLVM_ABI std::unique_ptr<Module> parseAssembly(
MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
SlotMapping *Slots = nullptr,
DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
return std::nullopt;
});
DataLayoutCallbackTy DataLayoutCallback =
[](StringRef, StringRef) { return std::nullopt; },
AsmParserContext *ParserContext = nullptr);

/// Parse LLVM Assembly including the summary index from a MemoryBuffer.
///
Expand Down Expand Up @@ -169,9 +171,9 @@ parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err);
LLVM_ABI bool parseAssemblyInto(
MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err,
SlotMapping *Slots = nullptr,
DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
return std::nullopt;
});
DataLayoutCallbackTy DataLayoutCallback =
[](StringRef, StringRef) { return std::nullopt; },
AsmParserContext *ParserContext = nullptr);

/// Parse a type and a constant value in the given string.
///
Expand Down
17 changes: 9 additions & 8 deletions llvm/include/llvm/IRReader/IRReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define LLVM_IRREADER_IRREADER_H

#include "llvm/ADT/StringRef.h"
#include "llvm/AsmParser/AsmParserContext.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Support/Compiler.h"
#include <memory>
Expand Down Expand Up @@ -50,19 +51,19 @@ getLazyIRFileModule(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
/// for it. Otherwise, attempt to parse it as LLVM Assembly and return
/// a Module for it.
/// \param DataLayoutCallback Override datalayout in the llvm assembly.
LLVM_ABI std::unique_ptr<Module> parseIR(MemoryBufferRef Buffer,
SMDiagnostic &Err,
LLVMContext &Context,
ParserCallbacks Callbacks = {});
LLVM_ABI std::unique_ptr<Module>
parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err, LLVMContext &Context,
ParserCallbacks Callbacks = {},
AsmParserContext *ParserContext = nullptr);

/// If the given file holds a bitcode image, return a Module for it.
/// Otherwise, attempt to parse it as LLVM Assembly and return a Module
/// for it.
/// \param DataLayoutCallback Override datalayout in the llvm assembly.
LLVM_ABI std::unique_ptr<Module> parseIRFile(StringRef Filename,
SMDiagnostic &Err,
LLVMContext &Context,
ParserCallbacks Callbacks = {});
LLVM_ABI std::unique_ptr<Module>
parseIRFile(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
ParserCallbacks Callbacks = {},
AsmParserContext *ParserContext = nullptr);
}

#endif
Loading
Loading