Skip to content

llvm-reduce: Reduce global variable code model #133865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions llvm/include/llvm/IR/GlobalVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
///
void setCodeModel(CodeModel::Model CM);

/// Remove the code model for this global.
///
void clearCodeModel();

// Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Value *V) {
return V->getValueID() == Value::GlobalVariableVal;
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/IR/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,15 @@ void GlobalVariable::setCodeModel(CodeModel::Model CM) {
assert(getCodeModel() == CM && "Code model representation error!");
}

void GlobalVariable::clearCodeModel() {
unsigned CodeModelData = 0;
unsigned OldData = getGlobalValueSubClassData();
unsigned NewData = (OldData & ~(CodeModelMask << CodeModelShift)) |
(CodeModelData << CodeModelShift);
setGlobalValueSubClassData(NewData);
assert(getCodeModel() == std::nullopt && "Code model representation error!");
}

//===----------------------------------------------------------------------===//
// GlobalAlias Implementation
//===----------------------------------------------------------------------===//
Expand Down
18 changes: 18 additions & 0 deletions llvm/test/tools/llvm-reduce/reduce-code-model.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: llvm-reduce -abort-on-invalid-reduction --delta-passes=global-values --test FileCheck --test-arg --check-prefix=INTERESTING --test-arg %s --test-arg --input-file %s -o %t.0
; RUN: FileCheck --implicit-check-not=define --check-prefix=RESULT %s < %t.0

; INTERESTING: @code_model_large_keep = global i32 0, code_model "large", align 4
; INTERESTING @code_model_large_drop = global i32 0

; RESULT: @code_model_large_keep = global i32 0, code_model "large", align 4{{$}}
; RESULT: @code_model_large_drop = global i32 0, align 4{{$}}
@code_model_large_keep = global i32 0, code_model "large", align 4
@code_model_large_drop = global i32 0, code_model "large", align 4

; INTERESTING: @code_model_tiny_keep = global i32 0, code_model "tiny", align 4
; INTERESTING @code_model_tiny_drop = global i32 0

; RESULT: @code_model_tiny_keep = global i32 0, code_model "tiny", align 4{{$}}
; RESULT: @code_model_tiny_drop = global i32 0, align 4{{$}}
@code_model_tiny_keep = global i32 0, code_model "tiny", align 4
@code_model_tiny_drop = global i32 0, code_model "tiny", align 4
3 changes: 2 additions & 1 deletion llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ void llvm::reduceGlobalValuesDeltaPass(Oracle &O, ReducerWorkItem &Program) {
if (GVar->isExternallyInitialized() && !O.shouldKeep())
GVar->setExternallyInitialized(false);

// TODO: Reduce code model
if (GVar->getCodeModel() && !O.shouldKeep())
GVar->clearCodeModel();
}
}
}
Loading