Skip to content

Commit

Permalink
early gdb support
Browse files Browse the repository at this point in the history
  • Loading branch information
drblallo committed Nov 25, 2024
1 parent 2c158a9 commit 920554d
Show file tree
Hide file tree
Showing 11 changed files with 738 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rlcAddLibrary(dialect src/Dialect.cpp src/Types.cpp src/Operations.cpp src/Conversion.cpp src/EmitMain.cpp src/TypeCheck.cpp src/Interfaces.cpp src/SymbolTable.cpp src/ActionArgumentAnalysis.cpp src/LowerActionPass.cpp src/LowerArrayCalls.cpp src/LowerToCf.cpp src/ActionStatementsToCoro.cpp src/OverloadResolver.cpp src/LowerIsOperationsPass.cpp src/InstantiateTemplatesPass.cpp src/LowerAssignPass.cpp src/EmitImplicitAssignPass.cpp src/LowerConstructOpPass.cpp src/EmitImplicitInitPass.cpp src/EmitImplicitDestructorInvocationsPass.cpp src/LowerForFieldOpPass.cpp src/EmitEnumEntitiesPass.cpp src/SortTypeDeclarationsPass.cpp src/AddOutOfBoundsCheckPass.cpp src/PrintIRPass.cpp src/ExtractPreconditionPass.cpp src/LowerAssertsPass.cpp src/AddPreconditionsCheckPass.cpp src/ActionLiveness.cpp src/UncheckedAstToDot.cpp src/RewriteCallSignaturesPass.cpp src/RemoveUselessAllocaPass.cpp src/MembeFunctionsToRegularFunctionsPass.cpp src/LowerInitializerListsPass.cpp src/Enums.cpp src/HoistAllocaPass.cpp src/RemoveUninitConstructsPass.cpp src/ConstraintsAnalysis.cpp src/TypeInterface.cpp src/SerializeRLPass.cpp src/Attrs.cpp)
rlcAddLibrary(dialect src/Dialect.cpp src/Types.cpp src/Operations.cpp src/Conversion.cpp src/EmitMain.cpp src/TypeCheck.cpp src/Interfaces.cpp src/SymbolTable.cpp src/ActionArgumentAnalysis.cpp src/LowerActionPass.cpp src/LowerArrayCalls.cpp src/LowerToCf.cpp src/ActionStatementsToCoro.cpp src/OverloadResolver.cpp src/LowerIsOperationsPass.cpp src/InstantiateTemplatesPass.cpp src/LowerAssignPass.cpp src/EmitImplicitAssignPass.cpp src/LowerConstructOpPass.cpp src/EmitImplicitInitPass.cpp src/EmitImplicitDestructorInvocationsPass.cpp src/LowerForFieldOpPass.cpp src/EmitEnumEntitiesPass.cpp src/SortTypeDeclarationsPass.cpp src/AddOutOfBoundsCheckPass.cpp src/PrintIRPass.cpp src/ExtractPreconditionPass.cpp src/LowerAssertsPass.cpp src/AddPreconditionsCheckPass.cpp src/ActionLiveness.cpp src/UncheckedAstToDot.cpp src/RewriteCallSignaturesPass.cpp src/RemoveUselessAllocaPass.cpp src/MembeFunctionsToRegularFunctionsPass.cpp src/LowerInitializerListsPass.cpp src/Enums.cpp src/HoistAllocaPass.cpp src/RemoveUninitConstructsPass.cpp src/ConstraintsAnalysis.cpp src/TypeInterface.cpp src/SerializeRLPass.cpp src/Attrs.cpp src/DebugInfo.cpp)
target_link_libraries(dialect PUBLIC rlc::utils MLIRSupport MLIRDialect MLIRLLVMDialect MLIRLLVMIRTransforms MLIRControlFlowDialect)

set(tblgen ${LLVM_BINARY_DIR}/bin/mlir-tblgen)
Expand Down
76 changes: 76 additions & 0 deletions lib/dialect/include/rlc/dialect/DebugInfo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once
/*
Copyright 2024 Massimo Fioravanti
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once

#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "rlc/dialect/Operations.hpp"

namespace mlir::rlc
{

class DebugInfoGenerator
{
public:
explicit DebugInfoGenerator(
mlir::ModuleOp op,
mlir::OpBuilder& rewriter,
mlir::TypeConverter* converter = nullptr,
mlir::DataLayout* dl = nullptr);
mlir::LLVM::DIFileAttr getFileOfLoc(mlir::Location loc) const;
mlir::LLVM::DITypeAttr getDIAttrOf(mlir::Type type) const;
mlir::LLVM::DISubprogramAttr getFunctionAttr(
mlir::rlc::FlatFunctionOp fun) const;
mlir::LLVM::DISubprogramAttr getFunctionAttr(
mlir::LLVM::LLVMFuncOp fun,
llvm::StringRef unmangledName = "",
mlir::FunctionType rlcType = nullptr,
bool artificial = false) const;

mlir::LLVM::DILocalVariableAttr getLocalVar(
mlir::Value value,
llvm::StringRef name,
mlir::FileLineColLoc location) const;

mlir::LLVM::DILocalVariableAttr getArgument(
mlir::Value value,
mlir::LLVM::DISubprogramAttr functionAttr,
llvm::StringRef name,
mlir::FileLineColLoc location) const;

private:
mlir::LLVM::DITypeAttr getDIAttrOfImpl(mlir::Type type) const;
mlir::LLVM::DISubprogramAttr makeFunctionAttr(
llvm::StringRef name,
llvm::StringRef mangledName,
mlir::LLVM::DISubroutineTypeAttr type,
bool declaration,
mlir::Location location,
bool artificial) const;
mutable mlir::ModuleOp op;
mlir::OpBuilder& rewriter;
mlir::TypeConverter* converter;
mlir::DataLayout* dl;

mlir::LLVM::DICompileUnitAttr compileUnit;

mutable DenseMap<mlir::Operation*, mlir::LLVM::DISubprogramAttr>
functionToSubProgram;
mutable DenseMap<mlir::Type, mlir::LLVM::DITypeAttr> typeToDITypeMap;
};

} // namespace mlir::rlc
94 changes: 89 additions & 5 deletions lib/dialect/src/Conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ limitations under the License.
*/

#include "llvm/ADT/TypeSwitch.h"
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Transforms/DialectConversion.h"
#include "rlc/dialect/DebugInfo.hpp"
#include "rlc/dialect/Operations.hpp"
#include "rlc/dialect/Passes.hpp"
#include "rlc/dialect/conversion/TypeConverter.h"
Expand Down Expand Up @@ -57,6 +59,46 @@ static mlir::LLVM::StoreOp makeAlignedStore(
loc, toStore, pointerTo, aligment);
}

class VarNameLowerer: public mlir::OpConversionPattern<mlir::rlc::VarNameOp>
{
public:
mlir::rlc::DebugInfoGenerator& diGenerator;
bool addDebugInfo;

VarNameLowerer(
mlir::TypeConverter& converter,
mlir::MLIRContext* ctx,
mlir::rlc::DebugInfoGenerator& diGenerator,
bool addDebugInfo)
: mlir::OpConversionPattern<mlir::rlc::VarNameOp>::OpConversionPattern(
converter, ctx),
diGenerator(diGenerator),
addDebugInfo(addDebugInfo)
{
}

mlir::LogicalResult matchAndRewrite(
mlir::rlc::VarNameOp op,
OpAdaptor adaptor,
mlir::ConversionPatternRewriter& rewriter) const final
{
if (addDebugInfo)
{
auto localVar = diGenerator.getLocalVar(
op.getValue(),
op.getName(),
op.getLoc().cast<mlir::FileLineColLoc>());
if (localVar)
{
rewriter.create<mlir::LLVM::DbgDeclareOp>(
op.getLoc(), adaptor.getValue(), localVar);
}
}
rewriter.eraseOp(op);
return mlir::success();
}
};

class NullLowerer: public mlir::OpConversionPattern<mlir::rlc::NullOp>
{
public:
Expand Down Expand Up @@ -1625,8 +1667,21 @@ class ConstantRewriter: public mlir::OpConversionPattern<mlir::rlc::Constant>
class FunctionRewriter
: public mlir::OpConversionPattern<mlir::rlc::FlatFunctionOp>
{
using mlir::OpConversionPattern<
mlir::rlc::FlatFunctionOp>::OpConversionPattern;
public:
mlir::rlc::DebugInfoGenerator& diGenerator;
bool addDebugInfo;

FunctionRewriter(
mlir::TypeConverter& converter,
mlir::MLIRContext* ctx,
mlir::rlc::DebugInfoGenerator& diGenerator,
bool addDebugInfo)
: mlir::OpConversionPattern<
mlir::rlc::FlatFunctionOp>::OpConversionPattern(converter, ctx),
diGenerator(diGenerator),
addDebugInfo(addDebugInfo)
{
}

mlir::LogicalResult matchAndRewrite(
mlir::rlc::FlatFunctionOp op,
Expand Down Expand Up @@ -1660,9 +1715,31 @@ class FunctionRewriter
newF.getBody().front().insertArgument(
unsigned(0), op.getType().getResults().front(), op.getLoc());

rewriter.eraseOp(op);

auto res = rewriter.convertRegionTypes(&newF.getRegion(), *typeConverter);
if (not op.isDeclaration() and addDebugInfo)
{
auto subprogramAttr = diGenerator.getFunctionAttr(
newF, op.getUnmangledName(), op.getType());
newF->setLoc(mlir::FusedLoc::get(
op.getContext(), { newF.getLoc() }, subprogramAttr));

rewriter.setInsertionPointToStart(&newF.getBody().front());
for (auto [argument, value, newValue] : llvm::zip(
op.getInfo().getArgs(),
op.getBody().front().getArguments(),
newF.getBody().front().getArguments()))
{
auto localVarAttr = diGenerator.getArgument(
value,
subprogramAttr,
argument.getName(),
value.getLoc().cast<mlir::FileLineColLoc>());
rewriter.create<mlir::LLVM::DbgDeclareOp>(
op.getLoc(), newValue, localVarAttr);
}
}

rewriter.eraseOp(op);

return mlir::success();
}
Expand Down Expand Up @@ -2007,6 +2084,9 @@ namespace mlir::rlc

mlir::TypeConverter realConverter;
mlir::rlc::registerConversions(realConverter, getOperation());
auto dl = mlir::DataLayout::closest(getOperation());
mlir::rlc::DebugInfoGenerator diGenerator(
getOperation(), rewriter, &realConverter, &dl);

mlir::TypeConverter converter;
converter.addConversion([&](mlir::Type t) -> mlir::Type {
Expand All @@ -2021,7 +2101,9 @@ namespace mlir::rlc
target.addIllegalDialect<mlir::rlc::RLCDialect>();

mlir::RewritePatternSet patterns(&getContext());
patterns.add<FunctionRewriter>(converter, &getContext())
patterns
.add<FunctionRewriter>(
converter, &getContext(), diGenerator, debug_info)
.add<TraitDeclarationEraser>(converter, &getContext())
.add<ValueUpcastRewriter>(converter, &getContext())
.add<EnumDeclarationEraser>(converter, &getContext())
Expand Down Expand Up @@ -2076,6 +2158,8 @@ namespace mlir::rlc
.add<ArrayAccessRewriter>(converter, &getContext())
.add<UninitializedConstructRewriter>(converter, &getContext())
.add<MemberAccessRewriter>(converter, &getContext())
.add<VarNameLowerer>(
converter, &getContext(), diGenerator, debug_info)
.add<ReferenceRewriter>(converter, &getContext())
.add<ClassDeclarationRewriter>(converter, &getContext())
.add<ExplicitConstructRewriter>(converter, &getContext())
Expand Down
Loading

0 comments on commit 920554d

Please sign in to comment.