Skip to content

Commit 5c43024

Browse files
authored
llvm-reduce: Reduce global variable code model (#133865)
The current API doesn't have a way to unset it. The query returns an optional, but the set doesn't. Alternatively I could switch the set to also use optional.
1 parent 347c5a7 commit 5c43024

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

llvm/include/llvm/IR/GlobalVariable.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
289289
///
290290
void setCodeModel(CodeModel::Model CM);
291291

292+
/// Remove the code model for this global.
293+
///
294+
void clearCodeModel();
295+
292296
// Methods for support type inquiry through isa, cast, and dyn_cast:
293297
static bool classof(const Value *V) {
294298
return V->getValueID() == Value::GlobalVariableVal;

llvm/lib/IR/Globals.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,15 @@ void GlobalVariable::setCodeModel(CodeModel::Model CM) {
557557
assert(getCodeModel() == CM && "Code model representation error!");
558558
}
559559

560+
void GlobalVariable::clearCodeModel() {
561+
unsigned CodeModelData = 0;
562+
unsigned OldData = getGlobalValueSubClassData();
563+
unsigned NewData = (OldData & ~(CodeModelMask << CodeModelShift)) |
564+
(CodeModelData << CodeModelShift);
565+
setGlobalValueSubClassData(NewData);
566+
assert(getCodeModel() == std::nullopt && "Code model representation error!");
567+
}
568+
560569
//===----------------------------------------------------------------------===//
561570
// GlobalAlias Implementation
562571
//===----------------------------------------------------------------------===//
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; 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
2+
; RUN: FileCheck --implicit-check-not=define --check-prefix=RESULT %s < %t.0
3+
4+
; INTERESTING: @code_model_large_keep = global i32 0, code_model "large", align 4
5+
; INTERESTING @code_model_large_drop = global i32 0
6+
7+
; RESULT: @code_model_large_keep = global i32 0, code_model "large", align 4{{$}}
8+
; RESULT: @code_model_large_drop = global i32 0, align 4{{$}}
9+
@code_model_large_keep = global i32 0, code_model "large", align 4
10+
@code_model_large_drop = global i32 0, code_model "large", align 4
11+
12+
; INTERESTING: @code_model_tiny_keep = global i32 0, code_model "tiny", align 4
13+
; INTERESTING @code_model_tiny_drop = global i32 0
14+
15+
; RESULT: @code_model_tiny_keep = global i32 0, code_model "tiny", align 4{{$}}
16+
; RESULT: @code_model_tiny_drop = global i32 0, align 4{{$}}
17+
@code_model_tiny_keep = global i32 0, code_model "tiny", align 4
18+
@code_model_tiny_drop = global i32 0, code_model "tiny", align 4

llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ void llvm::reduceGlobalValuesDeltaPass(Oracle &O, ReducerWorkItem &Program) {
7070
if (GVar->isExternallyInitialized() && !O.shouldKeep())
7171
GVar->setExternallyInitialized(false);
7272

73-
// TODO: Reduce code model
73+
if (GVar->getCodeModel() && !O.shouldKeep())
74+
GVar->clearCodeModel();
7475
}
7576
}
7677
}

0 commit comments

Comments
 (0)