Skip to content

[TableGen] Use const getter to implement non-const getter instead of the other way around. NFC #123452

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
Jan 18, 2025

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Jan 18, 2025

It's better to cast away constness on the reference being returned than to cast away constness on the this pointer.

…the other way around. NFC

It's better to cast away constness on the reference being returned than
to cast away constness on the this pointer.
@llvmbot
Copy link
Member

llvmbot commented Jan 18, 2025

@llvm/pr-subscribers-tablegen

Author: Craig Topper (topperc)

Changes

It's better to cast away constness on the reference being returned than to cast away constness on the this pointer.


Full diff: https://github.com/llvm/llvm-project/pull/123452.diff

1 Files Affected:

  • (modified) llvm/utils/TableGen/Common/CodeGenSchedule.h (+9-8)
diff --git a/llvm/utils/TableGen/Common/CodeGenSchedule.h b/llvm/utils/TableGen/Common/CodeGenSchedule.h
index d47c03514b155f..3fa7ca1a48cf0d 100644
--- a/llvm/utils/TableGen/Common/CodeGenSchedule.h
+++ b/llvm/utils/TableGen/Common/CodeGenSchedule.h
@@ -495,13 +495,14 @@ class CodeGenSchedModels {
     return ProcModels[I->second];
   }
 
-  CodeGenProcModel &getProcModel(const Record *ModelDef) {
+  const CodeGenProcModel &getProcModel(const Record *ModelDef) const {
     ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
     assert(I != ProcModelMap.end() && "missing machine model");
     return ProcModels[I->second];
   }
-  const CodeGenProcModel &getProcModel(const Record *ModelDef) const {
-    return const_cast<CodeGenSchedModels *>(this)->getProcModel(ModelDef);
+  CodeGenProcModel &getProcModel(const Record *ModelDef) {
+    return const_cast<CodeGenProcModel &>(
+        static_cast<const CodeGenSchedModels &>(*this).getProcModel(ModelDef));
   }
 
   // Iterate over the unique processor models.
@@ -529,14 +530,14 @@ class CodeGenSchedModels {
   const CodeGenSchedRW &getSchedRW(unsigned Idx, bool IsRead) const {
     return IsRead ? getSchedRead(Idx) : getSchedWrite(Idx);
   }
-  CodeGenSchedRW &getSchedRW(const Record *Def) {
+  const CodeGenSchedRW &getSchedRW(const Record *Def) const {
     bool IsRead = Def->isSubClassOf("SchedRead");
     unsigned Idx = getSchedRWIdx(Def, IsRead);
-    return const_cast<CodeGenSchedRW &>(IsRead ? getSchedRead(Idx)
-                                               : getSchedWrite(Idx));
+    return IsRead ? getSchedRead(Idx) : getSchedWrite(Idx);
   }
-  const CodeGenSchedRW &getSchedRW(const Record *Def) const {
-    return const_cast<CodeGenSchedModels &>(*this).getSchedRW(Def);
+  CodeGenSchedRW &getSchedRW(const Record *Def) {
+    return const_cast<CodeGenSchedRW &>(
+        static_cast<const CodeGenSchedModels &>(*this).getSchedRW(Def));
   }
 
   unsigned getSchedRWIdx(const Record *Def, bool IsRead) const;

Copy link
Contributor

@s-barannikov s-barannikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

return const_cast<CodeGenSchedModels *>(this)->getProcModel(ModelDef);
CodeGenProcModel &getProcModel(const Record *ModelDef) {
return const_cast<CodeGenProcModel &>(
static_cast<const CodeGenSchedModels &>(*this).getProcModel(ModelDef));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(note) Some argue that cost_cast is better suited for adding const.

@topperc topperc merged commit 2a4c4b5 into llvm:main Jan 18, 2025
10 checks passed
@topperc topperc deleted the pr/constcast branch January 18, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants