Skip to content

Commit

Permalink
[CIR][CIRGen] Change buildX functions to emitX (#1093)
Browse files Browse the repository at this point in the history
The buildX naming convention originated when the CIRGen implementation
was planned to be substantially different from original CodeGen. CIRGen
is now a much closer adaption of CodeGen, and the emitX to buildX
renaming just makes things more confusing, since CodeGen also has some
helper functions whose names start with build or Build, so it's not
immediately clear which CodeGen function corresponds to a CIRGen buildX
function. Rename the buildX functions back to emitX to fix this.

This diff was generated mostly mechanically. I searched for all buildX
functions in CIRGen and all emitX or buildX functions in CodeGen:

```
rg '\b[Bb]uild[A-Z][A-Za-z0-9_]*\b' clang/lib/CIR/CodeGen -Io | sort -u -o /tmp/buildfuncs
rg '\b([Ee]mit|[Bb]uild)[A-Z][A-Za-z0-9_]*\b' clang/lib/CodeGen -Io | sort -u -o /tmp/emitfuncs
```

I used a simple Python script to find corresponding functions:
https://gist.github.com/smeenai/02be7ced8564cef5518df72606ec7b19.
https://gist.github.com/smeenai/6ffd67be4249c8cebdd7fa99cfa4f13c is the
resulting list of correspondences. This isn't 100% accurate because it's
not accounting for the files that the functions are present in, but
that's pretty unlikely to matter here, so I kept it simple.

The buildX functions in CIRGen which correspond to an emitX function in
CodeGen should be changed, and the ones which correspond to a BuildX
function in CodeGen should not be changed. That leaves some functions
without any correspondences, which required a judgement call. I scanned
through all those functions, and buildVirtualMethodAttr was the only one
that seemed like it shouldn't be changed to emit. I performed the
replacement as follows:

```
funcs="$(awk '(/-> [Ee]/ || !/->/) && !/buildVirtualMethodAttr/ { print substr($1, 6) }' /tmp/corrfuncs | paste -sd '|')"
find clang/include/clang/CIR clang/lib/CIR/{CodeGen,FrontendAction} \( -name '*.h' -o -name '*.cpp' \) -print0 | \
  xargs -0 perl -pi -e "s/\bbuild($funcs)\\b/emit\\1/g"
```

The mechanical changes are in the first commit of this PR. There was a
manual fixup required for a token pasting macro in CIRGenExprScalar.cpp,
which is the second commit. I then ran `git clang-format`, which is the
third commit. (They'll be squashed together when the PR is committed.)
  • Loading branch information
smeenai authored Nov 9, 2024
1 parent 9abe35f commit 5e91e4f
Show file tree
Hide file tree
Showing 37 changed files with 2,522 additions and 2,561 deletions.
6 changes: 3 additions & 3 deletions clang/include/clang/CIR/CIRGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CIRGenerator : public clang::ASTConsumer {
~HandlingTopLevelDeclRAII() {
unsigned Level = --Self.HandlingTopLevelDecls;
if (Level == 0 && EmitDeferred)
Self.buildDeferredDecls();
Self.emitDeferredDecls();
}
};

Expand Down Expand Up @@ -101,8 +101,8 @@ class CIRGenerator : public clang::ASTConsumer {

bool verifyModule();

void buildDeferredDecls();
void buildDefaultMethods();
void emitDeferredDecls();
void emitDefaultMethods();
};

} // namespace cir
Expand Down
10 changes: 5 additions & 5 deletions clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ constexpr bool cirCConvAssertionMode =
namespace cir {

struct MissingFeatures {
// TODO(CIR): Implement the CIRGenFunction::buildTypeCheck method that handles
// TODO(CIR): Implement the CIRGenFunction::emitTypeCheck method that handles
// sanitizer related type check features
static bool buildTypeCheck() { return false; }
static bool emitTypeCheck() { return false; }
static bool tbaa() { return false; }
static bool cleanups() { return false; }
static bool emitNullabilityCheck() { return false; }
Expand Down Expand Up @@ -128,8 +128,8 @@ struct MissingFeatures {

// Missing Emissions
static bool variablyModifiedTypeEmission() { return false; }
static bool buildLValueAlignmentAssumption() { return false; }
static bool buildDerivedToBaseCastForDevirt() { return false; }
static bool emitLValueAlignmentAssumption() { return false; }
static bool emitDerivedToBaseCastForDevirt() { return false; }
static bool emitFunctionEpilog() { return false; }

// References related stuff
Expand Down Expand Up @@ -226,7 +226,7 @@ struct MissingFeatures {
static bool deferredReplacements() { return false; }
static bool shouldInstrumentFunction() { return false; }
static bool xray() { return false; }
static bool buildConstrainedFPCall() { return false; }
static bool emitConstrainedFPCall() { return false; }
static bool emitEmptyRecordCheck() { return false; }

// Inline assembly
Expand Down
58 changes: 29 additions & 29 deletions clang/lib/CIR/CodeGen/CIRAsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ static void collectInOutConstrainsInfos(const CIRGenFunction &cgf,
}
}

std::pair<mlir::Value, mlir::Type> CIRGenFunction::buildAsmInputLValue(
std::pair<mlir::Value, mlir::Type> CIRGenFunction::emitAsmInputLValue(
const TargetInfo::ConstraintInfo &Info, LValue InputValue,
QualType InputType, std::string &ConstraintStr, SourceLocation Loc) {

if (Info.allowsRegister() || !Info.allowsMemory()) {
if (hasScalarEvaluationKind(InputType))
return {buildLoadOfLValue(InputValue, Loc).getScalarVal(), mlir::Type()};
return {emitLoadOfLValue(InputValue, Loc).getScalarVal(), mlir::Type()};

mlir::Type Ty = convertType(InputType);
uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty);
Expand All @@ -226,9 +226,9 @@ std::pair<mlir::Value, mlir::Type> CIRGenFunction::buildAsmInputLValue(
}

std::pair<mlir::Value, mlir::Type>
CIRGenFunction::buildAsmInput(const TargetInfo::ConstraintInfo &Info,
const Expr *InputExpr,
std::string &ConstraintStr) {
CIRGenFunction::emitAsmInput(const TargetInfo::ConstraintInfo &Info,
const Expr *InputExpr,
std::string &ConstraintStr) {
auto loc = getLoc(InputExpr->getExprLoc());

// If this can't be a register or memory, i.e., has to be a constant
Expand All @@ -251,23 +251,23 @@ CIRGenFunction::buildAsmInput(const TargetInfo::ConstraintInfo &Info,

if (Info.allowsRegister() || !Info.allowsMemory())
if (CIRGenFunction::hasScalarEvaluationKind(InputExpr->getType()))
return {buildScalarExpr(InputExpr), mlir::Type()};
return {emitScalarExpr(InputExpr), mlir::Type()};
if (InputExpr->getStmtClass() == Expr::CXXThisExprClass)
return {buildScalarExpr(InputExpr), mlir::Type()};
return {emitScalarExpr(InputExpr), mlir::Type()};
InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
LValue Dest = buildLValue(InputExpr);
return buildAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr,
InputExpr->getExprLoc());
LValue Dest = emitLValue(InputExpr);
return emitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr,
InputExpr->getExprLoc());
}

static void buildAsmStores(CIRGenFunction &CGF, const AsmStmt &S,
const llvm::ArrayRef<mlir::Value> RegResults,
const llvm::ArrayRef<mlir::Type> ResultRegTypes,
const llvm::ArrayRef<mlir::Type> ResultTruncRegTypes,
const llvm::ArrayRef<LValue> ResultRegDests,
const llvm::ArrayRef<QualType> ResultRegQualTys,
const llvm::BitVector &ResultTypeRequiresCast,
const llvm::BitVector &ResultRegIsFlagReg) {
static void emitAsmStores(CIRGenFunction &CGF, const AsmStmt &S,
const llvm::ArrayRef<mlir::Value> RegResults,
const llvm::ArrayRef<mlir::Type> ResultRegTypes,
const llvm::ArrayRef<mlir::Type> ResultTruncRegTypes,
const llvm::ArrayRef<LValue> ResultRegDests,
const llvm::ArrayRef<QualType> ResultRegQualTys,
const llvm::BitVector &ResultTypeRequiresCast,
const llvm::BitVector &ResultRegIsFlagReg) {
CIRGenBuilderTy &Builder = CGF.getBuilder();
CIRGenModule &CGM = CGF.CGM;
auto CTX = Builder.getContext();
Expand Down Expand Up @@ -337,11 +337,11 @@ static void buildAsmStores(CIRGenFunction &CGF, const AsmStmt &S,
Dest = CGF.makeAddrLValue(A, Ty);
}

CGF.buildStoreThroughLValue(RValue::get(Tmp), Dest);
CGF.emitStoreThroughLValue(RValue::get(Tmp), Dest);
}
}

mlir::LogicalResult CIRGenFunction::buildAsmStmt(const AsmStmt &S) {
mlir::LogicalResult CIRGenFunction::emitAsmStmt(const AsmStmt &S) {
// Assemble the final asm string.
std::string AsmString = S.generateAsmString(getContext());

Expand Down Expand Up @@ -405,7 +405,7 @@ mlir::LogicalResult CIRGenFunction::buildAsmStmt(const AsmStmt &S) {
CGM.Error(S.getAsmLoc(), "multiple outputs to hard register: " + GCCReg);

OutputConstraints.push_back(OutputConstraint);
LValue Dest = buildLValue(OutExpr);
LValue Dest = emitLValue(OutExpr);

if (!Constraints.empty())
Constraints += ',';
Expand Down Expand Up @@ -496,8 +496,8 @@ mlir::LogicalResult CIRGenFunction::buildAsmStmt(const AsmStmt &S) {
mlir::Value Arg;
mlir::Type ArgElemType;
std::tie(Arg, ArgElemType) =
buildAsmInputLValue(Info, Dest, InputExpr->getType(),
InOutConstraints, InputExpr->getExprLoc());
emitAsmInputLValue(Info, Dest, InputExpr->getType(), InOutConstraints,
InputExpr->getExprLoc());

if (mlir::Type AdjTy = getTargetHooks().adjustInlineAsmType(
*this, OutputConstraint, Arg.getType()))
Expand Down Expand Up @@ -555,7 +555,7 @@ mlir::LogicalResult CIRGenFunction::buildAsmStmt(const AsmStmt &S) {
std::string ReplaceConstraint(InputConstraint);
mlir::Value Arg;
mlir::Type ArgElemType;
std::tie(Arg, ArgElemType) = buildAsmInput(Info, InputExpr, Constraints);
std::tie(Arg, ArgElemType) = emitAsmInput(Info, InputExpr, Constraints);

// If this input argument is tied to a larger output result, extend the
// input to be the same size as the output. The LLVM backend wants to see
Expand Down Expand Up @@ -676,8 +676,8 @@ mlir::LogicalResult CIRGenFunction::buildAsmStmt(const AsmStmt &S) {
} else if (ResultRegTypes.size() > 1) {
auto alignment = CharUnits::One();
auto sname = cast<cir::StructType>(ResultType).getName();
auto dest = buildAlloca(sname, ResultType, getLoc(S.getAsmLoc()),
alignment, false);
auto dest = emitAlloca(sname, ResultType, getLoc(S.getAsmLoc()),
alignment, false);
auto addr = Address(dest, alignment);
builder.createStore(getLoc(S.getAsmLoc()), result, addr);

Expand All @@ -692,9 +692,9 @@ mlir::LogicalResult CIRGenFunction::buildAsmStmt(const AsmStmt &S) {
}
}

buildAsmStores(*this, S, RegResults, ResultRegTypes, ResultTruncRegTypes,
ResultRegDests, ResultRegQualTys, ResultTypeRequiresCast,
ResultRegIsFlagReg);
emitAsmStores(*this, S, RegResults, ResultRegTypes, ResultTruncRegTypes,
ResultRegDests, ResultRegQualTys, ResultTypeRequiresCast,
ResultRegIsFlagReg);

return mlir::success();
}
Loading

0 comments on commit 5e91e4f

Please sign in to comment.