Skip to content

Eliminate vtr_free and vtr_malloc/vtr_calloc/vtr_realloc #3040

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

SamuelHo10
Copy link
Contributor

Replaced most of the free/malloc/calloc with new/delete in ArchFPGA library. I left a few unchanged because they either conflict with vtr::strdup or are used by other libraries such as VPR or Odin. This is my first pull request, so please let me know if I am forgetting anything.

@SamuelHo10 SamuelHo10 linked an issue May 14, 2025 that may be closed by this pull request
@github-actions github-actions bot added libarchfpga Library for handling FPGA Architecture descriptions lang-cpp C/C++ code labels May 14, 2025
@SamuelHo10 SamuelHo10 requested review from amin1377 and vaughnbetz May 15, 2025 14:22
Copy link
Contributor

@soheilshahrouz soheilshahrouz left a comment

Choose a reason for hiding this comment

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

Thanks @SamuelHo10

I left a few comments suggesting how the code can be improved in future PRs.

Copy link
Contributor

@vaughnbetz vaughnbetz left a comment

Choose a reason for hiding this comment

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

Thanks for starting the cleanup on this @SamuelHo10 .
I think some of these changes are unsafe in isolation though ... see my comments on calloc and default constructors. I think we need to write default constructors for the structs in question to be able to safely use new instead of calloc.
We should also take this as an opportunity to get rid of some complexity in the code and simplify memory management by moving to std:: container classes like vectors as @soheilshahrouz suggested. I think you should take this changes a little at a time though, running tests (or pushing to CI to have it run tests) frequently, and committing intermediate cleanups at each working stage.

We could discuss in person next week what good stages are.

@@ -944,7 +934,7 @@ void SyncModelsPbTypes_rec(t_arch* arch,

pb_type->model_id = model_match_prim_id;
vtr::t_linked_vptr* old = model_match_prim.pb_types;
model_match_prim.pb_types = (vtr::t_linked_vptr*)vtr::malloc(sizeof(vtr::t_linked_vptr));
model_match_prim.pb_types = new vtr::t_linked_vptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

We could probably change this linked list (which currently stores pb_type* pointers) to a vector of pb_type* (or maybe even just to direct pb_type elements). Probably don't do that yet though ... we should refactor in stages.

@vaughnbetz
Copy link
Contributor

It might be good enough to use = default to explicitly ask for default constructors, as I believe in that case all built-in type data members are initialized to 0 (e.g. int, float, etc.). That would only be a few lines of code, but you should test to be sure I'm right about the behaviour of that.
C++ has messed around a bit with the behaviour of constructors over the years, which is why the =default seems to change the behaviour of compiler-built default constructors.

@SamuelHo10 SamuelHo10 force-pushed the eliminate_free_and_malloc branch from 82756a1 to 0031a71 Compare May 20, 2025 16:48
@github-actions github-actions bot added the VPR VPR FPGA Placement & Routing Tool label May 20, 2025
@SamuelHo10 SamuelHo10 force-pushed the eliminate_free_and_malloc branch from 0031a71 to 904a0cd Compare May 21, 2025 17:56
@SamuelHo10 SamuelHo10 force-pushed the eliminate_free_and_malloc branch from 904a0cd to c0bf94c Compare May 21, 2025 17:59
@SamuelHo10 SamuelHo10 changed the title Eliminate vtr_free and vtr_malloc/vtr_calloc/vtr_realloc [WIP] Eliminate vtr_free and vtr_malloc/vtr_calloc/vtr_realloc May 21, 2025
@SamuelHo10 SamuelHo10 force-pushed the eliminate_free_and_malloc branch 2 times, most recently from f11da67 to b697f60 Compare May 23, 2025 18:34
@SamuelHo10 SamuelHo10 force-pushed the eliminate_free_and_malloc branch from b697f60 to e7b470a Compare May 23, 2025 18:46
@@ -2202,7 +2200,8 @@ struct t_arch {
LogicalModels models;

t_power_arch* power = nullptr;
t_clock_arch* clocks = nullptr;

std::shared_ptr<std::vector<t_clock_network>> clocks;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this a shared_ptr? Do we have multiple copies of this pointer? If not, I think unique_ptr would be a better choice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is shared by device_ctx.clock_arch. I think @vaughnbetz mentioned that in the future, we should get rid of the pointer entirely by assigning the value to device_ctx.clock_arch at the end. Currently, the code is creating two pointers with the same memory address and assigning a value after which can be dangerous.

@SamuelHo10 SamuelHo10 changed the title [WIP] Eliminate vtr_free and vtr_malloc/vtr_calloc/vtr_realloc Eliminate vtr_free and vtr_malloc/vtr_calloc/vtr_realloc May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang-cpp C/C++ code libarchfpga Library for handling FPGA Architecture descriptions VPR VPR FPGA Placement & Routing Tool
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Eliminate vtr_free and vtr_malloc/vtr_calloc/vtr_realloc
3 participants