So we (as in "various elements of US society") are paying to educate people, and then are sending them back where their home countries benefit, while the US does not. It's so dumb.
There are some cool exceptions, like some programs where students educated in the US are required to go back to their home countries. The idea is to help raise the level of education in countries where the higher education system isn't well developed or funded, and I think that sort of thing is an admirable goal. But it's so bizarre that we, for example, complain about supposed Chinese academic espionage, and complain about brain drain, but simultaneously make it really hard for international students to stay in the US after they graduate.
One PhD student costs a professor about $100K - $200K per year. A third of that is stipend and the other two thirds is... well, in CS I'm not even sure. A good bit of administrative welfare AFAICT. (Universities don't pay FICA taxes for grad students and grad student healthcare premiums are actually more of a subsidy for the risk pool of the aging faculty/admins than an actually half-reasonable benefit for the grad students.)
The US graduates 2K PhDs per year, of which >60% are foreign nationals. So we spend something roughly like (2000 * .6 people) * (150000 $/person) = $180 million per year. So a couple billion per decade.
Now... we do get folks with CS BS degrees for 1-5 years at $30K/yr, which is a fucking steal, and at least a great universities they are often from the upper quarter or so of the undergrad distribution. So maybe the students are also paying in the form of subsidized labor. But we do spend hundreds of millions per year in any case.
All of that said, as long as universities are staffing mathematics departments with shit paid ad juncts it's kind of hard to argue that our country is failing to compete due to a lack of phds...
So how do you want to account for labor costs? After they leave, the work got done and the workers got paid. Both sides benefit from this arrangement, though you can certainly argue (and many do) that the benefits should be split more fairly by paying the workers more.
Separately, the US would benefit from letting them stay and do more work. But that's generally the case for any immigration restriction. They get in the way of mutually beneficial private arrangements. US employers can't hire people who are willing to work for them, and the workers strongly benefit from being able to work in the US. Immigration restrictions are all about the government getting in people's way and the various justifications people make for continuing this interference.
Certainly, there has to be consequences to the countries losing minds & bodies.